java cxf webservice应用

天涯浪子 提交于 2020-01-25 07:48:54

一个小示例供大家参考:

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));
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!