Springboot整合webservice
Web Services介绍
WebService是什么
WebService是一种跨编程语言和跨操作系统平台的远程调用技术,服务之间的相互调用与开发语言无关
WebService平台技术
- XML+XSD
WebService采用HTTP协议传输数据,采用XML格式封装数据
- SOAP
WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。SOAP提供了标准的RPC方法来调用Web Service。
- WSDL
基于XML的语言,用于描述Web Service及其函数、参数和返回值。它是WebService客户端和服务器端都能理解的标准格式
提供服务
方案一:spring-boot-starter-web-services
- pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
- 接口服务和实现
@WebService
public interface MyService {
@WebResult
@WebMethod
String test();
@WebResult
@WebMethod
String test3();
}
// ======================================================================== //
@Slf4j(topic = "TEST")
@WebService(endpointInterface = "com.dimples.webservice.service.MyService")
public class MyServiceImpl implements MyService {
@Override
public String test3() {
log.info("MyServiceImpl中的show3方法被调用!");
return "【调用成功!】";
}
@Override
public String test() {
log.info("MyServiceImpl中的show方法被调用!");
return "【调用成功!】";
}
}
- 端点配置
@Configuration
public class WebServiceConfig {
@Bean
public Endpoint serviceImpl() {
MyService serviceImpl = new MyServiceImpl();
return Endpoint
.publish("http://localhost:8001/test", serviceImpl);
}
/*@Bean
public Endpoint my2Service() {
My2Service my2Service = new My2ServiceImpl();
return Endpoint
.publish("http://localhost:8001/test2", my2Service);
}*/
}
- 访问地址
http://localhost:8081/test?wsdl
maven编译错误:程序包com.sun.xml.internal.ws.spi不存在
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<bootclasspath>${JAVA_HOME}/jre/lib/rt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
// ============================= 或者 =========================================//
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.4</version>
</dependency>
参考链接:https://blog.csdn.net/weixin_45492007/article/details/99638002
方案一:cxf-spring-boot-starter-jaxws
- Springboot中已经有配置的webservice的jar包,我们在开发时直接引入即可
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.6</version>
</dependency>
项目中的Springboot父版本采用2.2.0
- webservice服务接口
@Service
@WebService(name = "MasterDataService", //该名字可自定义
targetNamespace = "http://webservice/system.biz.medical.com" // 该URL一般为当前包名的倒序
)
public interface EmrWebService {
/**
* 服务调用
*
* @param data String
* @return String
*/
@WebMethod
String emrService(@WebParam String data);
}
- 接口实现
@Slf4j
@Service
@WebService(name = "MasterDataService", // 与接口中的name相同
targetNamespace = "http://webservice/system.biz.medical.com", // 一般为当前包名的倒序
endpointInterface = "com.medical.biz.system.webservice.EmrWebService" // 为接口类的包名
)
public class EmrWebServiceImpl implements EmrWebService {
private static final String RESPONSE = "<Response><Header><SourceSystem>%s</SourceSystem><MessageID>%s</MessageID></Header><Body><ResultCode>%s</ResultCode><ResultContent>%s</ResultContent></Body></Response>";
@Override
public String emrService(@WebParam String data) {
log.info("接收参数 => [ {} ]", data);
if (data.isEmpty()) {
return "传入的参数为空";
}
return String.format(RESPONSE, "01", "", "0", "成功");
}
}
- 配置cxf服务发布
注意下方代码中注释信息中的坑点
@Configuration
public class CxfConfig {
private Bus bus;
private EmrWebService emrWebService;
@Autowired
public CxfConfig(Bus bus, EmrWebService emrWebService) {
this.bus = bus;
this.emrWebService = emrWebService;
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, emrWebService);
endpoint.publish("/MasterDataService");
return endpoint;
}
// 配置多个类中的webservice服务
@Bean
public Endpoint endpointDrug() {
EndpointImpl endpoint = new EndpointImpl(bus, drugWebService);
endpoint.publish("/DrugWebService");
return endpoint;
}
/**
* 坑点:
* 1、方法名为dispatchServlet()
* 如果Springboot的主版本在2.0.X以下时,可以正常启动,此时在方法中配置的访问路径将会覆盖默认或者在application.properties文件中配置server.servlet.context-path=中的值
* 如果Springboot的主版本在2.0.X以上时,此时启动报错,不能正常启动
* 此时需要将方法名更改,不能用dispatchServlet(),在方法中配置webservice的访问路径,不会与项目配置的全局访问路径冲突,
*
* @return ServletRegistrationBean
*/
@SuppressWarnings("all")
@Bean
public ServletRegistrationBean disServlet() {
// 此处配置的是webservice接口的访问地址,类似 http://127.0.0.1:8001/emr
return new ServletRegistrationBean(new CXFServlet(), "/emr/*");
}
}
- 启动项目,访问
http://127.0.0.1:8001/emr,可以看到服务信息

推荐使用soapUI进行服务测试,结果如下:

问题总结
- org.apache.cxf.interceptor.Fault: Unmarshalling Error: 意外的元素 (uri:"", local:"Request")。所需元素为(none)
可以将参数包在 内

浙公网安备 33010602011771号