Web environment uses relative path to publish Webservice

Web environment uses relative path to publish Webservice

 

         Usually, the publishing address of our Webservice service will be a relative path. When using it with Spring , we need to introduce the schema of the Cxf configuration Webservice , such as jaxws , to define the corresponding Webservice .

 

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:jaxws="http://cxf.apache.org/jaxws"

      xsi:schemaLocation="

         http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd

         http://cxf.apache.org/jaxws

         http://cxf.apache.org/schemas/jaxws.xsd">

 

   <beanid="helloWorld"class="com.elim.test.cxf.service.HelloWorldServiceImpl"/>

 

   <!-- ID can be slashed or not -->

   <jaxws:serverid="/helloWorld"serviceBean="#helloWorld"serviceClass="com.elim.test.cxf.service.HelloWorldService"/>

 

</beans>

 

         Then we need to define a CXFServlet to receive the corresponding Webservice request, as shown below.

 

<?xmlversion="1.0"encoding="UTF-8"?>

 

<web-app 

        version="3.0" 

        xmlns="http://java.sun.com/xml/ns/javaee" 

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

       

   <context-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath:webservices.xml</param-value>

   </context-param>

  

   <listener>

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   </listener>

  

   <servlet>

      <servlet-name>CxfServlet</servlet-name>

      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

   </servlet>

  

   <servlet-mapping>

      <servlet-name>CxfServlet</servlet-name>

      <url-pattern>/sys/service/*</url-pattern>

   </servlet-mapping>

  

</web-app>

 

         In this way, if we want to access the HelloWorldService Webservice , we can access it through /sys/service/helloWorld relative to the current project release path .

         但是如果我们的工程没有使用Spring,但是我们也想把Webservice发布为工程的某一个路径的相对路径怎么办?Cxf也为我们提供了相应的支持,这比使用Spring时的CXFServlet要麻烦一点。Cxf为我们提供了一个CXFNonSpringServlet,这要求我们实现自己的继承自CXFNonSpringServletServlet,然后重写其loadBus方法,最终需要达到的效果就是CXFNonSpringServlet使用的Bus与我们的发布Webservice时使用的Bus是同一个即可。这样当我们访问CXFNonSpringServlet请求某个Webservice时,CXFNonSpringServlet将自动根据请求的相对路径去寻找发布路径为其相对路径的Webservice

 

publicclass WebserviceServlet extends CXFNonSpringServlet {

 

   /**

    *

    */

   private static final longserialVersionUID = 3919868434043901738L;

 

   @Override

   protected void loadBus(ServletConfig sc) {

      super.loadBus(sc);

      //获取当前CXFNonSpringServlet使用的Bus,然后利用该Bus来发布服务

      Bus bus = this.getBus();

      JaxWsServerFactoryBean jsFactoryBean = new JaxWsServerFactoryBean();

      jsFactoryBean.setBus(bus);

      //该路径是相对于当前CXFNonSpringServlet匹配的路径的,也可以加斜杠

      jsFactoryBean.setAddress("helloWorld");

      jsFactoryBean.setServiceClass(HelloWorldService.class);

      jsFactoryBean.setServiceBean(new HelloWorldServiceImpl());

      jsFactoryBean.create();

   }

  

}

 

   这时我们的web.xml需要定义我们自己的CXFNonSpringServlet,即:

 

<?xml version="1.0" encoding="UTF-8"?>

 

<web-app 

        version="3.0" 

        xmlns="http://java.sun.com/xml/ns/javaee" 

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

       

   <servlet>

      <servlet-name>CxfServlet</servlet-name>

      <servlet-class>com.elim.test.cxf.servlet.WebserviceServlet</servlet-class>

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

   </servlet>

  

   <servlet-mapping>

      <servlet-name>CxfServlet</servlet-name>

      <url-pattern>/sys/service/*</url-pattern>

   </servlet-mapping>

  

</web-app>

 

参考文档

http://cxf.apache.org/docs/servlet-transport.html

 

(注:本文是基于Cxf2.7.6所写)

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326806455&siteId=291194637