一個小示例供大家參考:
1、依賴引入:
<!--WerbService CXF依賴--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.2.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.2.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.2.4</version> </dependency>
2、創建服務端
@javax.jws.WebService public interface MyWebService { @WebMethod String sayHello(String user); }
@javax.jws.WebService public class MyWebServiceImpl implements MyWebService { @Override public String sayHello(String user) { return "我是服務端返回值:"+user; } }
public void activateCabinet(){ String url = "http://localhost:9028/wsServeice"; Endpoint.publish(url,new MyWebServiceImpl()); System.out.println("發佈webService成功!"); }
3、生成客戶端
進入jdk bin目錄中使用cmd窗口生成客戶端代碼:
wsimport -keep -s . http://localhost:9028/wsServeice?wsdl
生成成功之後在當前目錄中生成客戶端相關代碼
將生成的文件拷貝到當前應用的SRC目錄中進行調用即可:
public static void main(String[] args){ JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean(); factoryBean.setServiceClass(MyWebService.class); factoryBean.setAddress("http://localhost:9028/wsServeice?wsdl?wsdl"); MyWebService myWebService =(MyWebService) factoryBean.create(); Gson gson = new Gson(); String s = gson.toJson("kkkk"); System.out.println(myWebService.sayHello(s)); }