A simple example of generating and using WebService in Java

Source: Internet
Author: User

Java uses Metro to generate WebService, publish WebService in Tomcat, and how to use WebService

A simple example:

 

Download and install Metro.(This example uses 1.5)
Http://java.net/projects/metro
After downloading the package for the metro-1.5.jar, run the Java-jar metro-1.5.jar
A Metro folder is generated under the directory where Metro. jar is located, which contains the packages we want to use.

Export package: the following five packages.
/Metroft-5core/webservices-api.jar
// Metro/webservices-extra.jar
// Metro/webservices-extra-api.jar
// Metro/webservices-rt.jar
// Metro/webservices-tools.jar
(You can copy this package to Tomcat's servers/lib to ensure that there is no error in WebService)

 

Entity class: User
Package COM. you. me. web. entity; </P> <p> public class user {</P> <p> private string userid = ""; <br/> private string username = ""; <br/> private string nickname = ""; <br/> private string email = ""; <br/> private string Password = ""; <br/> private string registtime = ""; <br/> private string statusid = ""; <br/>}</P> <p>

Write the WebService to be published.
For example, in the user service userservice, you can obtain the user object based on the user ID.
Package COM. you. me. WS; </P> <p> Import javax. JWS. webmethod; <br/> Import javax. JWS. webService; </P> <p> Import COM. you. me. web. entity. user; </P> <p>/** <br/> * @ author you. me.2011-4-23 <br/> */<br/> @ WebService (servicename = "userservicews", <br/> portname = "userserviceinstance ", <br/> name = "userservice", <br/> targetnamespace = "http://www.mylitboy.com/youme ") <br/> public class userserviceimpl {<br/> @ webmethod <br/> public user getuserbyid (string ID) {<br/> return new user ("1 ", "ABC", "ABCD", "abc@abc.com"); <br/>}</P> <p>

Note:
Servicename is not written. The default value is class name + service, such as userserviceimplservice.
Name is not written. The default value is the class name, such as userserviceimpl.
Portname is not written. The default value is class name + port, for example, userserviceimplport.
If targetnamespace is not written, the default package name, such as (http://ws.me.you.com)

Configuration: Web. xml
Add the following statement to Web. xml.

<Listener> <br/> <listener-class> COM. sun. XML. WS. transport. HTTP. servlet. wsservletcontextlistener </listener-class> <br/> </listener> </P> <p> <servlet> <br/> <servlet-Name> helloservice </servlet-name> <br/> <servlet-class> COM. sun. XML. WS. transport. HTTP. servlet. wsservlet </servlet-class> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> helloservice </servlet-Name> <br/> <URL-pattern>/WS/* </url-pattern> <br/> </servlet-mapping> </P> <p>

Note:
URL-pattern: the access address you need to listen to by listener.

 

Configure sun-jaxws.xml
Create a WEB-INF file in the sun-jaxws.xml.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <endpoints version = "2.0" <br/> xmlns = "http://java.sun.com/xml/ns/jax-ws/ri/runtime" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/jax-ws/ri/runtime http://java.sun.com/xml/ns/jax-ws/ri/runtime/sun-jaxws.xsd"> <br/> <endpoint implementation = "com. you. me. WS. userserviceimpl "<br/> URL-pattern ="/WS/a "name =" userservice "> </Endpoint> <br/> <endpoint implementation =" com. you. me. WS. userserviceimpl2 "<br/> URL-pattern ="/WS/AB "name =" userservice2 "> </Endpoint> <br/> </endpoints> </P> <p>Note:
If multiple WebServices exist, write multiple endpoints.
Implementation: WebService class
URL-pattern: the access address, which must be within the range of URL-pattern in Web. xml.
Name: Random write

 

Deploy the project to Tomcat.
Visit the following address to view the corresponding WebService information:
Http: // localhost: 8116/youme/WS/
Http: // localhost: 8116/youme/WS/? WSDL

 

Use wsimport to generate WebService code

Then enter the wsimport command in cmd to generate the WebService-related code.
C:/users/you. Me> wsimport-s d: // temp http: /localhost: 8116/youme/WS/? WSDL
The following statement indicates that the operation is successful.
Parsing WSDL...
Generating code...
Compiling code...

In this case, you can see some Java classes under D:/temp:
D:/temp/COM/mylitboy/youme
Getuserbyid. Java
Getuserbyidresponse. Java
Objectfactory. Java
User. Java
Userservice. Java
Userservicews. Java

Therefore, the WebService on the server is complete.

Start using the generated WebService.

We can package these Java classes into a jar file. It can also be copied directly to other projects for use.

Usage:
Copy the folder to the new project (that is, the project you want to use WebService ).
/Youmews_test/COM/mylitboy/youme/getuserbyid. Java
/Youmews_test/COM/mylitboy/youme/getuserbyidresponse. Java
......

Write test code:
Test. Java
Package SS; </P> <p> Import COM. mylitboy. youme. userservice; <br/> Import COM. mylitboy. youme. userservicews; </P> <p>/** <br/> * @ author you. me.2011-4-23 <br/> */<br/> public class test {<br/> Public static void main (string [] ARGs) {<br/> userservice US = new userservicews (). getuserserviceinstance (); <br/> system. out. println (US. getuserbyid ("1"); <br/>}</P> <p>

Note:

Here, the userservice is the name attribute value in @ WebService.

Userservicews is the servicename attribute value in @ WebService.

 
This completes the entire process.

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.