Introduction to creating and releasing Web Services for JAX-WS integration web engineering)

Source: Internet
Author: User
Tags webservice annotation

 

First of all, in order to our ws experiment can be successfully completed, it is best to go to the official website to download the full version of the JAX-WS tool. The downloaded JAR file needs to be decompressed and installed by running the jar command in the Windows command line. For example, I have jaxws2.1.1 _ 20070501.jar. After installation, you can get a directory with many jar files under Lib. The bin directory contains several commands that are very useful, such as wsgen and wsimport. Of course, I mentioned that these two commands are already available in JDK 6.

Now we have started the experiment.

 Server

1. Create a web project and release it to tomcat in the future. The new web project named myws2 is located in the Directory D: \ workspace \ myws2,Create a new folder WSDL under the root directory of this project

2, for convenience, pilot into these packages: jaxws-rt.jar, jaxb-api.jar, jaxb-impl.jar, jaxb-xjc.jar, streambuffer. jar, stax-ex.jar, these jar packages are just the jar package needed to run the following simplest experiment, the actual operation, if you need more jar package is also very normal, these jar packages are all from Jax-ws.

3. Create a class in the web project:

package leon.ws;import javax.jws.WebService;@WebServicepublic class Hello {public String sayHello(String name) {return ("Welcome, I am Server. Hello, " + name);}public int sum(int a, int b) {return a + b;}}

OK, very simple. There is only one @ WebService annotation, And neither method in the class uses the @ webmethod annotation. In this way, both methods will actually become web service methods.

4. An important step is to use the wsgen tool to generate the WSDL and server-side helper classes. Open the doscommand line, go to the root directory of the web project on the server, and run the following command:


Wsgen-cp d: \ workspace \ myws2 \ webroot \ WEB-INF \ Classes Leon. ws. Hello-WSDL-s Src-r WSDL

Note:The path here is very important. If your path is different, you must correct it. The wsgen command uses JDK 6, so you must configure the JDK environment variables. If you use a lower JDK version, you do not have this tool, you need to configure the tool under the bin that downloaded the installed JAX-WS to the environment variable path.

After the program runs successfully, refresh the WSDL directory under the web project directory to view two files, one XSD and one WSDL. Refresh SRC to see that the jaxws package and four classes are generated, that is, the above hello. in Java, each method generates two classes.

5. Create a New WEB-INF under the sun-jaxws.xml, the content is as follows:

<?xml version="1.0" encoding="UTF-8"?><endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">  <endpoint name="Hello" implementation="leon.ws.Hello" url-pattern="/HelloService" />  </endpoints>

This defines the WS endpoint. The implementation class corresponds to the hello. Java defined above. The two attributes name and URL-pattern are defined by yourself, but it is better to be reasonable.

6. modify web. XML to configure a listener, and bind our hello. Java and Its Method to the servlet with a servlet. The content is as follows:

<listener><listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class></listener><servlet><servlet-name>Hello</servlet-name><servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>Hello</servlet-name><url-pattern>/HelloService</url-pattern></servlet-mapping>

The directory structure of the project is as follows:

Publish it to Tomcat and run it. The server web project will be loaded by Tomcat.

After Tomcat is started successfully. Browser access: http: // 10.225.112.86: 8888/myws2/helloservice? WSDL

IfThe browser displays the content of the WSDL FileThe release is successful. The server is complete.

Note:Tomcat is started on the local lan ip address, so the access address is also IP. If you use localhost, it is okay.

Client:

For more information about how the server code is organized and created, see my other article: "JAX-WS a very simple RPC-style Web Service synchronous call"

Because we can access the server's WSDL file, the client can access the server's ws through various channels.

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.