一个小示例供大家参考:
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)); }
来源:CSDN
作者:songqping
链接:https://blog.csdn.net/songqping/article/details/103778408