码上敲享录 > java入门知识分享 > CXF实现webservice(不使用spring)

CXF实现webservice(不使用spring)

上一章章节目录下一章 2019-02-22已有1942人阅读 评论(0)

CXF实现webservice(不使用spring)


解决方法:

1.使用的jar包

asm-5.0.4.jar

cxf-core-3.1.18.jar

cxf-rt-bindings-soap-3.1.18.jar

cxf-rt-databinding-jaxb-3.1.18.jar

cxf-rt-frontend-jaxws-3.1.18.jar

cxf-rt-frontend-simple-3.1.18.jar

cxf-rt-transports-http-3.1.18.jar

cxf-rt-transports-udp-3.1.18.jar

cxf-rt-ws-addr-3.1.18.jar

cxf-rt-wsdl-3.1.18.jar

neethi-3.1.1.jar

slf4j-api-1.7.25.jar

stax2-api-3.1.4.jar

woodstox-core-asl-4.4.1.jar

wsdl4j-1.6.3.jar

wss4j-bindings-2.1.12.jar

xmlschema-core-2.2.3.jar


2.服务端实现代码

import javax.jws.WebMethod;

import javax.jws.WebService;

import javax.xml.ws.BindingType;

import javax.xml.ws.Endpoint;

@WebService(targetNamespace="http://test.com/")

public class WebServiceServer implements WebServiceServerI{

//@WebMethod

   public String sayHello(String name){

       return "Hello"+name;

   }

 

}


3.服务端接口代码

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebService;

import javax.xml.ws.BindingType;

@WebService(targetNamespace="http://test.com/")

public interface WebServiceServerI {

public String sayHello(String name);

}


4.服务发布

import javax.servlet.ServletConfig;

import javax.xml.ws.Endpoint;

import org.apache.cxf.BusFactory;

import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

import com.test.WebServiceServer;

public class WebServiceServlet extends CXFNonSpringServlet {

private static final long serialVersionUID = 1L;

@Override

protected void loadBus(ServletConfig sc) {

super.loadBus(sc);  

WebServiceServer hi=new WebServiceServer();

       BusFactory.setDefaultBus(getBus());

       Endpoint.publish("/hi", hi);

}

}


在web.xml添加:

<servlet>

 <servlet-name>cxfServlet</servlet-name>

 <servlet-class>com.servlet.WebServiceServlet</servlet-class>

 <load-on-startup>1</load-on-startup>

 </servlet>

 <servlet-mapping>

 <servlet-name>cxfServlet</servlet-name>

 <url-pattern>/ws/*</url-pattern>

 </servlet-mapping>


浏览器访问:http://localhost:8080/httpSMS2/ws/hi?wsdl试试,有东西出来说明发布成功


5.客户端调用

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

       //http://localhost:8080/httpSMS2是web项目的访问地址

      org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/httpSMS2/ws/hi?wsdl");

     Object[] objects;

       try {

         objects = client.invoke("sayHello",new Object[]{"admin"});

         //输出调用结果

         System.out.println(objects[0].toString());

       } catch (Exception e) {

         e.printStackTrace();

       }

更多客户端调用代码:http://www.yayihouse.com/yayishuwu/chapter/1787

0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交