1、maven依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.4</version>
</dependency>
2、spring集成cxf webservice
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean class="org.apache.cxf.jaxws.context.WebServiceContextImpl">
</bean>
<!--其中id是自己定义的,implementor是接口实现类,address就是访问的地址 -->
<!-- 相当于Endpoint.publish("http://localhost:8080/service", newHelloServiceImp()); -->
<bean class="org.apache.cxf.jaxws.context.WebServiceContextImpl">
</bean>
<jaxws:endpoint implementor="com.wisdombud.product.sharedplatform.entity.service.impl.EntityListWebServiceImpl"
address="/queryList">
</jaxws:endpoint>
<jaxws:endpoint implementor="com.wisdombud.product.sharedplatform.entity.service.impl.EntityDetailWebserviceImpl"
address="/queryDetail">
</jaxws:endpoint>
</beans>
3、接口
import javax.jws.WebService;
/**
* 功能 :
* date : 2019-06-11 10:47
*
* @version : 0.0.4-snapshot
* @Author : txzhang@wisdombud.com
* @since : JDK 1.8
*/
@WebService
public interface EntityListWebService {
String getListData(Long appId, String key, Integer start, Integer limit);
}
@WebService
@Service
public class EntityListWebServiceImpl implements EntityListWebService {
@Autowired
private WebServiceContext webServiceContext;
@Override
public String getListData(final Long appId, final String key, final Integer start, final Integer limit) {
//获取request对象
final HttpServletRequest request = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
...业务代码
return "张三";
}
}
4、webservice拦截器实现
import java.util.HashMap;
/**
* 功能 : webservice 拦截器
* date : 2019-06-11 11:52
*
* @version : 0.0.4-snapshot
* @Author : txzhang@wisdombud.com
* @since : JDK 1.8
*/
public class WebserviceInterceptor extends AbstractPhaseInterceptor<Message> {
public WebserviceInterceptor() {
//需要把握好拦截的时机
super(Phase.PRE_INVOKE);
}
@Override
public void handleMessage(final Message message) throws Fault {
//用来加载调用者传递的参数
final HashMap map = message.getContent(HashMap.class);
System.out.println(map.toString());
}
}
5、客户端测试,我这里通过cxf,写的main方法测试的
/**
* 功能 :
* date : 2019-06-11 14:52
*
* @version : 0.0.4-snapshot
* @Author : txzhang@wisdombud.com
* @since : JDK 1.8
*/
public class CxfClientTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean ();
factoryBean.setAddress("http://localhost/ws/queryList?wsdl");
factoryBean.setServiceClass(EntityListWebService.class);
final EntityListWebService service = (EntityListWebService) factoryBean.create();
System.out.println(service.getListData(11648L, "bed1bd3b-4bde-4d0c-9dca-94fab3893260", 0, 10));
}
}