JAX-WS + Spring development WebService

Source: Internet
Author: User

Through a few days of time to study the use of JAX-WS to develop WebService, read some information on the Internet summed up the development of Jax-WS is probably divided into two kinds.

The following items use Spring3.0,jar package can be downloaded to the official website

First: Use a separate port (refers to a port that can be customized in spring)

First of all, the first way, this way does not need to add an additional jar package, he is using the JDK comes with the JWS to achieve.

Web. xml file Configuration:

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"ID= "webapp_id"version= "2.5">    <Display-name>Jaxwsexample</Display-name>        <!--The applicationcontext*.xml file is in the Conf folder in the SRC directory -    <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Classpath:conf/applicationcontext*.xml</Param-value>    </Context-param>        <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>    <!--log4j Log -    <Listener>        <Listener-class>Org.springframework.web.util.Log4jConfigListener</Listener-class>    </Listener>    <!--Prevent memory leaks -       <Listener>        <Listener-class>Org.springframework.web.util.IntrospectorCleanupListener</Listener-class>    </Listener></Web-app>

Applicationcontext-jaxws.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-2.5.xsd ">    <Context:annotation-config/>    <Context:component-scanBase-package= "Com.example.ws"></Context:component-scan><!--The value address of the baseaddress and the port number is custom, and the port number is not used--<Beanclass= "Org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">        < Propertyname= "BaseAddress"value= "http://localhost:8088/" />    </Bean>    </Beans>

Java Code

 Packagecom.example.ws;ImportJavax.jws.WebMethod;ImportJavax.jws.WebService;Importjavax.jws.soap.SOAPBinding;ImportJavax.jws.soap.SOAPBinding.Style;ImportOrg.springframework.stereotype.Service;//Spring Annotations Use@Service ("Exampleservice")//WebService Address Use@WebService (servicename= "Example")//preventing JDK version issues@SOAPBinding (style=style.rpc) Public classExampleservice {//Dependency Dao/service//@Autowired//private Ibasedao Basedao;@WebMethod Publicstring example1 (String request) {System.out.println (request); String Response= Request + "Hello"; returnresponse; }}

Java code in the method of writing is relatively simple, you can also write input parameters and output parameters as an object, which is determined according to the needs.

The above is the first method of implementation, the local access address is directly: http://localhost:8088/example?wsdl

The second way: using the servlet mode, this way uses the server port

In this way, you need to rely on the jar files in Jax-ws 2.2, and you need to download additional Jaxws-spring-1.8.jar and Xbean-spring-3.0.jar

  

Xml

<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.5"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_2_5.xsd">    <welcome-file-list>        <Welcome-file>index.jsp</Welcome-file>    </welcome-file-list>    <!--to end to configure the start spring container -    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>        <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Classpath:conf/applicationcontext*.xml</Param-value>    </Context-param>    <!--END -    <!--used to configure the Address bar request path -    <servlet>        <Servlet-name>Jaxwsservlet</Servlet-name>        <Servlet-class>Com.sun.xml.ws.transport.http.servlet.WSSpringServlet</Servlet-class>    </servlet>    <servlet-mapping>        <Servlet-name>Jaxwsservlet</Servlet-name>        <Url-pattern>/services/*</Url-pattern>    </servlet-mapping></Web-app>

Applicationcontext-jaxws.xml, unlike before, requires the XML header to increase the claims of WSS

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:ws= "Http://jax-ws.dev.java.net/spring/core"Xmlns:wss= "Http://jax-ws.dev.java.net/spring/servlet"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-2.5.xsd Http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd/http Jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd ">    
<!--scanning Spring annotations--<Context:annotation-config/> <Context:component-scanBase-package= "Com.example.ws"> </Context:component-scan>
<!--binding WebService address, which needs to correspond to the address of Web. xml<wss:bindingURL= "/services/add"> <Wss:service> <Ws:serviceBean= "#exampleService" /> </Wss:service> </wss:binding>

</Beans>

Java Code

 Packagecom.example.ws;ImportJavax.jws.WebMethod;ImportJavax.jws.WebService;Importjavax.jws.soap.SOAPBinding;ImportJavax.jws.soap.SOAPBinding.Style;ImportOrg.springframework.stereotype.Service; @Service ("Exampleservice")
Only here is different from method one @webservice@soapbinding (style=style.rpc) Public classExampleservice {@WebMethod Publicstring example1 (String request) {System.out.println (request); String Response= request+ "Hello"; returnresponse; }}

The local request address in this way is: http://localhost:8080/JAXWsExample2/services/add?wsdl

HTTP://IP Address: Server port number/Project application name/servlet define address? wsdl

Both of these ways I can use the Tomcat service locally to test through

There is only a second available test on the WebSphere server

JAX-WS + Spring development WebService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.