springboot整合webservice采用CXF技术
转载自:https://blog.csdn.net/qq_31451081/article/details/80783220
强推:https://blog.csdn.net/chjskarl/article/details/52052771、
最好: https://blog.csdn.net/xie19900123/article/details/83986482
第一种:
依赖:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.5</version> </dependency>
/** * WebService接口 * @作者 Administrator * @创建日期 2018/6/23 0023 * @创建时间 11:21. */ @WebService(name = "CommonService", // 暴露服务名称 targetNamespace = "http://www.WebService.demo.example.com") //命名空间,一般是接口的包名倒序 public interface CommonService { @WebMethod @WebResult(name = "String",targetNamespace = "") public String HelloWorld(@WebParam(name = "HelloName") String name); }
/**接口实现 * @作者 Administrator * @创建日期 2018/6/23 0023 * @创建时间 11:26. */ @WebService(serviceName = "CommonService",//与前面接口一致 targetNamespace = "http://www.WebService.demo.example.com", //与前面接口一致 endpointInterface = "com.webservice.demo.webservice.CommonService") //接口地址 @Component public class CommonServiceImpl implements CommonService { @Override public String HelloWorld(String name) { return "Hello World!!! --->"+name; } }
public class CxfClient { public static void main(String[] args) { cl1(); } /** * 方式1.代理类工厂的方式,需要拿到对方的接口 */ public static void cl1() { try { // 接口地址 String address = "http://localhost:8080/services/CommonService?wsdl"; // 代理工厂 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // 设置代理地址 jaxWsProxyFactoryBean.setAddress(address); // 设置接口类型 jaxWsProxyFactoryBean.setServiceClass(CommonService.class); // 创建一个代理接口实现 CommonService cs = (CommonService) jaxWsProxyFactoryBean.create(); // 数据准备 String userName = "Leftso"; // 调用代理接口的方法调用并返回结果 String result = cs.HelloWorld(userName); System.out.println("返回结果:" + result); } catch (Exception e) { e.printStackTrace(); } }
@Configuration public class WebConfig { @Autowired private Bus bus; @Autowired CommonService service; /*jax-ws*/ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, service); endpoint.publish("/CommonService"); return endpoint; }
第2种: