Publish Webservice and Webservice using relative paths in the Web Environment
The publishing address of our Webservice is usually a relative path. When used with Spring, we need to introduce Cxf to configure the Webservice schema, such as jaxws, to define the corresponding Webservice.
<? Xml version ="1.0"Encoding =UTF-8"?>
<Beans xmlns =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">
<Bean id ="HelloWorld"Class ="Com. elim. test. cxf. service. HelloWorldServiceImpl"/>
<! -- ID can be added with a slash or not -->
<Jaxws: server id ="/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.
<? 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">
<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.
However, if our project does not use Spring, but we also want to publish Webservice as the relative path of a specific path of the project, what should we do? Cxf also provides corresponding support for us, which is a little more troublesome than CXFServlet when using Spring. Cxf provides us with a CXFNonSpringServlet, which requires us to implement our own Servlet inherited from CXFNonSpringServlet, and then rewrite its loadBus method, the final result is that the Bus used by CXFNonSpringServlet is the same as the Bus used for Webservice release. In this way, when we access CXFNonSpringServlet to request a Webservice, CXFNonSpringServlet will automatically find the Webservice with the publishing path as its relative path based on the relative path of the request.
Java background framework springmvc mybaits set Code Generator
PublicClassWebserviceServletExtendsCXFNonSpringServlet {
/**
*
*/
PrivateStaticFinalLongSerialVersionUID= 3919868434043901738L;
@ Override
ProtectedVoidLoadBus (ServletConfig SC ){
Super. LoadBus (SC );
// Obtain the current Bus used by CXFNonSpringServlet, and then use this Bus to publish the service
Bus bus =This. GetBus ();
JaxWsServerFactoryBean jsFactoryBean =NewJaxWsServerFactoryBean ();
JsFactoryBean. setBus (bus );
// This path is matched with the current CXFNonSpringServlet path. You can also add a slash.
JsFactoryBean. setAddress ("helloWorld ");
JsFactoryBean. setServiceClass (HelloWorldService.Class);
JsFactoryBean. setServiceBean (NewHelloWorldServiceImpl ());
JsFactoryBean. create ();
}
}
In this case, our web. xml needs to define our own CXFNonSpringServlet, namely:
<? 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>