(original) Implement simple SOAP specification with jax-ws+spring WebService

Source: Internet
Author: User
Tags soap wsdl

Reprint Please specify source: http://www.cnblogs.com/Starshot/p/7050084.html

Soap is a simple Object access protocol, it can also be understood as a specification for communication between programs, mainly based on XML and HTTP, and because of XML and HTTP based, so this protocol is very versatile, can be very good to achieve communication between different language platforms. The disadvantage is the relative weight level.

There are many soap frameworks in Java, such as XFIRE,CXF. When it comes to soap, you have to mention rest because these concepts are easy to confuse.

Rest is a structural style, and the design that implements restful style is called restful. It advocates more lightweight, stateless, self-describing, and more fully leverages the features of HTTP itself, such as get,post,put,delete, to achieve more efficient performance and make interactions more concise and understandable. For example, to get a book in a collection of AAA, you can directly through the URI:/BOOKS/AAA to express.

While soap is requested via post, there is no other way to make full use of HTTP, only HTTP is used as a transport protocol, and the resource information that needs to be obtained is included in the post XML message.

It is important to note that rest is a style that is not limited to a framework of a program. For example, the CXF framework can implement both restful webservice and webservice of the SOAP specification. For example, in my other blog post, the CXF simple example (http://www.cnblogs.com/Starshot/p/6889751.html) can be seen as a restful webservice.

Now it's time to implement the super-simple webservice of a SOAP specification with Jax-WS, the lightweight framework (which is then combined with spring).

First, build a WebService Web project, which serves as the provider of the service. Then build a HelloService class, as follows:

Then the code for the class is as follows:

 PackageCom.webservice;ImportJavax.jws.WebService;Importjavax.xml.ws.Endpoint; @WebService Public classHelloService { Publicstring Sayit (String str) {return"Say it:" +str; }         Public Static voidMain (string[] args) {endpoint.publish ("Http://localhost:9009/hello/HelloService",NewHelloService ()); System.out.println ("Publish done!"); }}

is not very simple, is not very pleasantly surprised, and then directly executed, appearing publish done! Then open the browser, enter HTTP://LOCALHOST:9009/HELLO/HELLOSERVICE?WSDL, the WSDL file information appears, even if it is successful.

Next, build a client project as follows:

This time you need to generate a WSDL client, right-click on the project, and then create a new WebService client:

Click Next and enter the address you just entered in the browser:

Then click Next to set the path of the code after pressing OK, after success as shown:

Where Com.webservice inside is the client program. Then, as a new test class test1, the contents are as follows:

 Packagecom.test;Importjava.rmi.RemoteException;Importjavax.xml.rpc.ServiceException;ImportCom.webService.HelloService;ImportCom.webService.HelloServiceProxy;ImportCom.webService.HelloServiceServiceLocator; Public classTest1 { Public Static voidMain (string[] args)throwsserviceexception, remoteexception {helloservice hs=NewHelloserviceproxy (); System.out.println (Hs.sayit ("yohoo!! ")); }}

Execute, the program, console output: yohoo!! , it means success.

By looking at the client code, you know that helloserviceproxy,helloserviceservicelocator,helloserviceportbindingstub three classes can actually be used to invoke a service. And all classes are eventually implemented through the Sayit method in the Helloserviceportbindingstub class, Helloserviceproxy and Helloserviceservicelocator are actually the helloserviceportbindingstub of the further processing, which is also an embodiment of the proxy model.

Then, the above-mentioned way to publish the service is simple, but in the actual application is certainly not such a sub-publishing service, the actual must be published in the web. JAX-WS and Spring combine to publish a WebService service:

First add the following jar packages to the original Webserviec project:

Commons-logging-1.0.4.jar
Gmbal-api-only.jar
Ha-api.jar
Jaxb-impl.jar
Jaxws-api.jar
Jaxws-rt.jar
Jaxws-spring-1.8.jar
Management-api.jar
Policy.jar
Spring-beans-3.2.14.release.jar
Spring-context-3.2.14.release.jar
Spring-core-3.2.14.release.jar
Spring-expression-3.2.14.release.jar
Spring-web-3.2.14.release.jar
Stax-ex.jar
Streambuffer.jar
Xbean-spring-3.0.jar

Because the jar package is more involved, if necessary, you can leave the mailbox, I sent the past.

Then a Speakingservice class is built, and the component annotation indicates that the class will be managed by the spring container, as shown below,

 Package Com.webservice; Import Javax.jws.WebService; Import org.springframework.stereotype.Component, @Component @webservice  Public class Speakingservice  {        public  string Speak (string content) {        return "speaking:" +  Content;    }}

Then build the Web. xml file under Web-inf, which reads as follows:

<!DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_ 3.dtd "><Web-app>    <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Classpath:applicationContext.xml</Param-value>    </Context-param>    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>   <servlet>  <Servlet-name>Jwsservice</Servlet-name>  <Servlet-class>Com.sun.xml.ws.transport.http.servlet.WSSpringServlet</Servlet-class>  <Load-on-startup>1</Load-on-startup></servlet><servlet-mapping>  <Servlet-name>Jwsservice</Servlet-name>  <Url-pattern>/speakingservice</Url-pattern> </servlet-mapping>
</Web-app>

The Applicationcontext.xml file is then created under the SRC path, which reads as follows:

<Beansxmlns= "Http://www.springframework.org/schema/beans"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"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ spring-beans-2.5.xsdhttp://jax-ws.dev.java.net/spring/corehttp://jax-ws.dev.java.net/spring/core.xsdhttp:// jax-ws.dev.java.net/spring/servlethttp://jax-ws.dev.java.net/spring/servlet.xsdhttp://www.springframework.org/ Schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">  <Context:component-scanBase-package="*"/>  <wss:bindingURL= "/speakingservice">  <Wss:service>   <Ws:serviceBean= "#speakingService" />  </Wss:service> </wss:binding></Beans> 

Here is a detail to note is <ws:service bean= "#speakingService"/> Here the Speakingservice first letter remember to lowercase, although the original class name is uppercase beginning, but by default loaded into the spring container , the Bean's ID is a lowercase start.

The end is to use Tomcat to start the project, the IP and port access and resource names are based on the Tomcat container settings. The method for generating the client is the same as the one that was just started.

If there is a problem, please indicate communication.

Reprint Please specify source: http://www.cnblogs.com/Starshot/p/7050084.html

(original) Implement simple SOAP specification with jax-ws+spring 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.