Spring Boot + cxf 整合WebService服务端和客户端
springboot 1.5.9
cxf的pom依赖
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.12</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.12</version>
</dependency>
<!-- CXF webservice -->
服务端
接口代码
@WebService
public interface ServiceDemo {
@WebMethod
public @WebResult(name="out") String getValue(@WebParam(name = "param")String param);
@WebMethod
public @WebResult(name="out") String getStatus();
}
接口实现
@WebService(serviceName = "ServiceDemo", // 与接口中指定的name一致
targetNamespace = "http://service.xxx.com/", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.xxx.service.ServiceDemo"// 接口地址
)
public class ServiceDemoImpl implements ServiceDemo{
@Override
public String getValue(String param) {
//你的业务逻辑
//TODO:
}
@Override
public String getStatus() {
//你的业务逻辑
//TODO:
}
}
cxf配置类
@Configuration
public class CxfConfig {
@Bean("dispatcherServletCxf")
public ServletRegistrationBean dispatcherServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/webService/services/*");//发布服务名称
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public ServiceDemo ServiceDemo() {
return new ServiceDemoImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), ServiceDemo());
endpoint.publish("/demo");
return endpoint;
}
}
这里需要注意的我自定义注册了一个DispatcherServlet一定要去一个其他的名字,如果也用dispatcherServlet取名就会替换掉springboot自动注入的DispatcherServlet,下面给出springboot提供的DispatcherServlet的源码
public ServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet, new String[]{this.serverProperties.getServletMapping()});
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
if(this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
}
从源码中可以看到springboot自动注入的DispatcherServlet取名是dispatcherServlet
接下来启动项目在浏览器中输入http://localhost:18081/webService/services/demo?wsdl
wsdl文件如图
客服端
/**
* 动态调用
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();
Client client=dcflient.createClient("http://localhost:18081/webService/services/demo?wsdl");
Object[] objects;
String param="<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" +
"<info>\n" +
"\n" +
" <CorporationCode>63</CorporationCode>\n" +
"\n" +
"<Time>2019-01-17</Time>\n" +
"<api name=\"testParam\"></api>\n" +
"</info>";
try {
objects = client.invoke("getValue",param);
System.out.println("返回结果:"+objects[0].toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("出错了"+e.toString());
}
}
//调用方式二,通过接口协议获取数据类型
public static void main2(String[] args) throws Exception {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setAddress("http://localhost:18081/webService/services/demo?wsdl");
jaxWsProxyFactoryBean.setServiceClass(ServiceDemo.class);
ServiceDemo userService=(ServiceDemo)jaxWsProxyFactoryBean.create();
String param="<?xml version=\"1.0\" encoding=\"gb2312\"?>\n" +
"<info>\n" +
"\n" +
" <CorporationCode>63</CorporationCode>\n" +
"\n" +
"<Time>2019-01-17</Time>\n" +
"<api name=\"testParam\"></api>\n" +
"</info>";
String result= userService.getValue(param);
// System.out.println(userService.getStatus());
System.out.println("结果1:"+result);
}