本文为自己开发遇到的问题做一个记录,用的是springboot,发布的时候2种方式同时发布wsdl的接口,调用方式有些不同。
1. xfire
使用jdk自带的注解的方式创建webservice,JAX-WS jdk创建
xfire方式需要一个xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 发布webservice -->
<bean id="xfire.serviceRegistry"
class="org.codehaus.xfire.service.DefaultServiceRegistry"/>
<bean id="xfire.transportManager"
class="org.codehaus.xfire.transport.DefaultTransportManager"
init-method="initialize" destroy-method="dispose">
</bean>
<bean id="xfire" class="org.codehaus.xfire.DefaultXFire">
<constructor-arg index="0">
<ref bean="xfire.serviceRegistry" />
</constructor-arg>
<constructor-arg index="1">
<ref bean="xfire.transportManager" />
</constructor-arg>
</bean>
<bean id="xfire.typeMappingRegistry"
class="org.codehaus.xfire.aegis.type.DefaultTypeMappingRegistry"
init-method="createDefaultMappings" >
</bean>
<bean id="xfire.aegisBindingProvider"
class="org.codehaus.xfire.aegis.AegisBindingProvider">
<constructor-arg index="0">
<ref bean="xfire.typeMappingRegistry" />
</constructor-arg>
</bean>
<bean id="xfire.serviceFactory"
class="org.codehaus.xfire.service.binding.ObjectServiceFactory">
<constructor-arg index="0">
<ref bean="xfire.transportManager" />
</constructor-arg>
<constructor-arg index="1">
<ref bean="xfire.aegisBindingProvider" />
</constructor-arg>
</bean>
<bean id="xfire.servletController"
class="org.codehaus.xfire.transport.http.XFireServletController">
<constructor-arg>
<ref bean="xfire" />
</constructor-arg>
</bean>
<bean id="xfire.messageServiceFactory"
class="org.codehaus.xfire.service.binding.ObjectServiceFactory">
<constructor-arg index="0" ref="xfire.transportManager" />
<constructor-arg index="1" ref="xfire.messageBindingProvider" />
<property name="style" value="message" />
</bean>
<bean id="xfire.messageBindingProvider"
class="org.codehaus.xfire.service.binding.MessageBindingProvider" />
<!-- START SNIPPET: xfire -->
<!-- <bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/orderService">
<ref bean="orderService" />
</entry>
</map>
</property>
</bean> -->
<!-- 定义一个父类,让所有服务都可共用,而不需要每次都加上XFire的配置 -->
<bean id="XFireAbstractBean"
class="org.codehaus.xfire.spring.remoting.XFireExporter"
abstract="true">
<property name="serviceFactory">
<ref bean="xfire.serviceFactory" />
</property>
<property name="xfire">
<ref bean="xfire" />
</property>
</bean>
</beans>
暴露的接口
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService( name = "DocService ",// 暴露服务名称
targetNamespace = "http://webservice.kong.com"// 命名空间,一般是接口的包名倒序
)
public interface DocService {
@WebMethod
@WebResult(name = "arg0", targetNamespace = "http://webservice.kong.com")
public String DocServicePAT(@WebParam(name = "arg0") String arg0);
}
暴露接
口的实现
@WebService(serviceName = "DocServiceImpl ", // 与接口中指定的name一致
targetNamespace = "http://webservice.kong.com", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.kong.webservice.DocServiceImpl "// 接口地址
)
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
@Service
@Component
@Slf4j
public class DocServiceImpl implements DocService{
@Override
public String acceptDocumentTask(String request) {
}
}
Config配置
@Configuration
@ImportResource(locations = {"classpath:applicationContext.xml"})
public class WsdlConfig {
@Autowired
private DocService DocService ;
//xfire发布接口 需要搭配ImportResource(locations = {"classpath:applicationContext.xml"}), 需在发布的Service和Service实现类都加
/**
@WebService(serviceName = "DocService ", // 与接口中指定的name一致
targetNamespace = "http://webservice.kong.com", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.kong.webservice.DocService"// 接口地址
)
*/
// //
@Bean
public ServletRegistrationBean<XFireSpringServlet> xfireServlet() {
ServletRegistrationBean<XFireSpringServlet> registrationBean = new ServletRegistrationBean<>(new XFireSpringServlet(),"/ws/*");
registrationBean.setName("Xfire");
return registrationBean;
}
@Bean
public WebAnnotations webAnnotations() {
return new Jsr181WebAnnotations();
}
@Bean
public Jsr181HandlerMapping jsr181HandlerMapping(@Autowired XFire xFire) {
Jsr181HandlerMapping jsr181HandlerMapping = new Jsr181HandlerMapping();
jsr181HandlerMapping.setXfire(xFire);
jsr181HandlerMapping.setWebAnnotations(webAnnotations());
jsr181HandlerMapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
return jsr181HandlerMapping;
}
}
2. cxf
导入cxf的相关jar
Config配置
@Configuration
public class WsdlConfig {
@Autowired
private WSService WSService ;
@Autowired
private DocService DocService ;
cxf发布接口 搭配Endpoint
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean(name = "wsBean")
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/ws/*");///发布路径配置
return wbsServlet;
}
@Bean
public Endpoint endpointPurchase() {
EndpointImpl endpoint = new EndpointImpl( springBus(), WSService );
endpoint.publish("/NCCloud_WS_Service");
// System.out.println("服务发布成功!地址为:http://127.0.0.1:90011/TIMS/ws/WSService ?wsdl");
return endpoint;
}
@Bean
public Endpoint endpoint2() {
EndpointImpl endpoint = new EndpointImpl(springBus(), DocService );
endpoint.publish("/NC_WS_Service");
// System.out.println("服务发布成功!地址为:http://127.0.0.1:90011/TIMS/ws/DocService ?wsdl");
return endpoint;
}
cxf发布接口
}
接口配置
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
* @Author kdc
**/
@WebService(name = "WSService", // 暴露服务名称
targetNamespace = "http://WebService.kong.com"// 命名空间,一般是接口的包名倒序
)
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface WSService {
@WebMethod
@WebResult(name = "arg0", targetNamespace = "http://WebService.kong.com")
public String acceptDocsk(@WebParam(name = "arg0") String arg0);
}
接口实现类
@Service
@Component
public class WSServiceImpl implements WSService {
@Override
public String acceptDocsk(String req) {
return "false";
}
}