1.項目使用springboot 2.1.3版本,集成webservice使用的依賴如下
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.1.3.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent> 7 <!-- cxf webservice --> 8 <dependency> 9 <groupId>org.apache.cxf</groupId> 10 <artifactId>cxf-spring-boot-starter-jaxws</artifactId> 11 <version>3.2.4</version> 12 </dependency>
2.編寫服務接口
1 package com.example.demo.service; 2 3 import javax.jws.WebMethod; 4 import javax.jws.WebService; 5 6 @WebService 7 public interface WebServiceTest { 8 9 @WebMethod //聲明暴露服務的方法,可以不寫 10 public String getMsg(String msg); 11 }
3.編寫業務方法
package com.example.demo.service.impl; import java.util.Random; import javax.jws.WebService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.example.demo.service.WebServiceTest; @WebService(targetNamespace="http://service.demo.example.com/", endpointInterface="com.example.demo.service.WebServiceTest") @Component public class WebServiceTestImpl implements WebServiceTest { private static final Logger log = LoggerFactory.getLogger(WebServiceTestImpl.class); @Override public String getMsg(String msg) { log.info("客戶端發來的參數:{}",msg); String serviceMsg = "hello,I'm server client."+new Random().nextLong(); return serviceMsg; } }
注意:接口和接口實現類不在同一包下
4.發布服務類
1 package com.example.demo.config; 2 3 import javax.xml.ws.Endpoint; 4 5 import org.apache.cxf.Bus; 6 import org.apache.cxf.jaxws.EndpointImpl; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.context.annotation.Configuration; 10 11 import com.example.demo.service.WebServiceTest; 12 13 @Configuration 14 public class CxfConfig { 15 16 @Autowired 17 private Bus bus; 18 19 @Autowired 20 private WebServiceTest webServiceTest; 21 22 @Bean 23 public Endpoint endpoint() { 24 EndpointImpl endpoint = new EndpointImpl(bus, webServiceTest); 25 endpoint.publish("/user"); 26 return endpoint; 27 } 28 }
5.遠程訪問的地址:http://ip:端口/項目路徑/services/user?wsdl 來查看你的wsdl文件
6.客戶端調用
在另一項目里,一樣需要引入上面的依賴
<!-- cxf webservice --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.4</version> </dependency>
然后:
package com.example.demo; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringWebserviceClientApplicationTests { @Test public void contextLoads() { } @Test public void test() { // 創建動態客戶端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8080/services/user?wsdl"); Object[] objects = new Object[0]; try { objects = client.invoke("getMsg", "hello,我是客戶端哦!"); System.out.println("返回數據:" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } } }
提示:我是在springboot測試包里面測試的,你也可以直接寫個main方法進行測試。
接下來敲黑板划重點:
1.@WebService(targetNamespace="http://service.demo.example.com/", endpointInterface="com.example.demo.service.WebServiceTest") 這個注解里面targetNamespace是一定要寫的,這是指名我們暴露出去的接口在哪,不寫映射不到,就會報No operation was found with the name {...}這個錯誤 ,endpointInterface="com.example.demo.service.WebServiceTest" 這個是告訴接口是哪個
2.請注意targetNamespace最后面有一個斜線,我就是因為沒加斜線,老是報上面那個錯誤,真的是弄了一天,有時候一個小小的符號就夠你忙活一天的了。深痛的教訓啊。。。。。
3.遠程訪問地址:http://xxxx/services/user?wsdl 后面加粗部分是固定寫法,當然你要是重新配置了這個wsdl的訪問路徑,就需要使用你配置的,這里就不寫怎么配置了,感覺沒啥用。。。,有感興趣的可以百度搜搜看
最后,有不對的歡迎指出糾正!