How to quickly publish the WebService service in Java

Source: Internet
Author: User
Tags wsdl

In practice, have you ever experienced the need to publish webservice to someone else's call? If you are a novice in Java, the customer or partner is very tight, you certainly worry about it. Don't worry, let's see what tools or objects we can use to publish. There are too many ways to choose in Java, such as Axis2,XFire, JWS , and so on. Depressed, see can not understand, who know which one to use.

Don't worry, if you are very anxious , the customer will have to, then use the following method:

First, through the template provided by Axis2 automatic release

This method is very simple, just download the axis package from the inside of the "Axis-1_4\webapps" to find the axis site, copy it out to the tomcat/webapps you want to deploy, and then Put the Java class you want to publish in the Axis folder, and then change the suffix of the file name to "JWs" so that a webservice is published successfully. Access is possible by following the path "http://IP:Port/axis/class name. jws?wsdl".

What do you think? Is this method very simple? Yes, it's especially useful when you're learning Java or not having a good SDE development and debugging tool at hand.

But obviously, this method is not very good to debug, but does not highlight your professional standards, but also can not be integrated into your development environment. So if you're not in a hurry, you can publish it in one of the following ways:

Ii. Manual release via JWs

The steps are as follows:

1. Create a new dynamic Web Project in Eclipse, as shown (believe that you are familiar with this step, so you can skip it):


2. Enter the project name in "project name", such as "Javawebservice":


3. Click the "Next" button until the following dialog box appears:


in the above dialog box must be aware of the need to select "Generate Web. XML Deployment Descriptor", or later add is more troublesome. When selected, click the Finish button to complete the project creation.

4. Introduce the necessary jar packages:
Axis.jar, Commons-discovery-0.2.jar, Commons-logging.jar, Jaxrpc.jar, Saaj.jar, Wsdl4j.jar

< Span style= "Color:rgb (85,85,85); Font-family: Song body, ' Arial Narrow ', Arial,serif; font-size:10px; line-height:28px ">    It is best to copy the jar package into the project Web-inf/lib directly after the jar package in the project, which will not cause the project folder or jar package folder to be read without the problem of the jar package:



< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; line-height:28px ">    " OK "button to complete the jar package reference.

< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; line-height:28px ">< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; line-height:28px ">    5, new package and Java file as shown in:

< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; line-height:28px ">< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; line-height:28px ">< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; Line-height:28px ">

< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; line-height:28px ">< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; line-height:28px ">    first talk about Com.test.javabean.Student.java this class. It can be seen that this class is a javabean to encapsulate a Java entity class, and the advantage of this is that in case a lot of variables are returned to the client, the client does not declare many parameters in the method to receive them, but rather to receive them through an object. The Student.java is implemented as follows:

< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; line-height:28px ">< Span style= "font-family: Song Body, ' Arial Narrow ', Arial,serif; font-size:10px; Line-height:28px ">

Package Com.test.javabean;import Java.io.serializable;public class Student implements Serializable{private String ID; private string Name;private string sex;private int age;public string GetID () {return ID;} public void SetID (String ID) {id = ID;} Public String GetName () {return Name;} public void SetName (String name) {name = name;} Public String Getsex () {return Sex;} public void Setsex (String sex) {sex = sex;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

Then look at the Com.test.webservice.CStudent.java class, which is defined as follows:

Package Com.test.webservice;import Javax.jws.webservice;import com.test.javabean.Student; @WebServicepublic class cstudent {public Student getstudent (String name) throws Java.rmi.remoteexception{student Student = new Student (); Student . SetName (name); Student.setage (+); Student.setid ("+"); Student.setsex ("Male"); return student;}}
For the sake of simplicity, do not involve more background query, first write dead return results. I wonder if you notice that the difference between this class and the normal class is that a "@WebService" declaration is added to the top of the class. Yes, this is a WebService method, is it also very simple? But it's a good thing to remember to introduce the Javax.jws.WebService package. Even right, this way is JDK1.7 in the way, so to use this feature, you have to trouble to upgrade the JDK to 1.7, the method is very simple, with the green version of the can, do not bother you to unload the JDK and reload. Only need to put the JDK to the root of the disk (this is to prevent the path of a space or parentheses, otherwise, if the problem you are forced), and then the JDK path is defined as Java_home, and then add%java_home%/bin in Path; The advantage of this is that you can easily change the JDK version at any time without having to uninstall and reinstall.

Pulled away. With the WebService method body, you also need to publish the WebService, so you have to create a new Com.test.servlet.CStudentServlet.java class:

Package Com.test.servlet;import Java.io.ioexception;import Java.net.inetaddress;import Java.net.unknownhostexception;import Java.util.properties;import Javax.jws.webservice;import javax.servlet.*; Import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.xml.ws.Endpoint ; Import Com.test.webservice.cstudent;public class Cstudentservlet extends httpservlet{/** * @see Servlet#init ( ServletConfig) */public void init (servletconfig config) throws servletexception {//TODO auto-generated method Stubsuper. Init (config); System.out.println ("Starting WebService Service: Http://192.168.56.1:8082/JavaWebService/Student"); Endpoint.publish ("Http://<span style=" font-family:arial, Helvetica, Sans-serif; " >192.168.56.1</span><span style= "font-family:arial, Helvetica, Sans-serif;" >:8082/</span><span style= "font-family:arial, Helvetica, Sans-serif;" >javawebservice&Lt;/span><span style= "font-family:arial, Helvetica, Sans-serif;" >/student ", New Cstudent ()); </span>}/** * @see servlet#getservletconfig () */public ServletConfig Getservletconfig () {//TODO auto-generated method Stubreturn null;} /** * @see Httpservlet#doget (httpservletrequest request, httpservletresponse response) */protected void Doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated Method stub}public void Service (ServletRequest servletrequest, Servletresponse servletresponse) throws Servletexception, IOException {System.out.println ("Access to Wsservlet Service () method");} /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) */protected void DoPost ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated Method stub}}

In the above code there is a detail I do not know you notice: The IP address, port number, site name and other information is written to die in the code. Yes, not only that, what you write here IP, you have to use what IP access, even if it is written in native localhost can not. JWs is so strange. So in practice, we can refer to some data to obtain the IP address and port number dynamically.

By the way, is there an error in your code? Yes, that's because you have to do one more thing--Add the server and a reference to the server component package, in Tomcat, for example, by doing the following:

Locate the Servers window and click the "New Server Wizard ..." Link:

Open the following dialog box:

Did you see that? Select "Tomcat v7.0 Server" under Apache, and then click "Next":

Click the "Browse" button on the right to locate the directory where Tomcat is located, and then select 1.7 jdk in the JRE. If you have not configured the JDK, please find the data to configure it, some here will not talk about, or else pull away.

Then go to this window:

This dialog has to pay special attention, do not worry click the "Add" button added site that, otherwise you will not find the location of the site deployment. So this step directly clicks "Finish". When you are finished, double-click the newly added server to open the following dialog box:


In the above window, be sure to first select the second item "Use Tomcat installation (takes control of Tomcat installation)" and then change the content to "WebApps" in "Deploy path", Then press the "Ctrl+s" key to save the configuration so that you can deploy the site to the WebApps of your designated Tomcat.

Then remember to add a reference to the site Servlet-api.jar package, right-click on the project, select "Properties" to open the following dialog box:


Did you see that? After switching to the "Libraries" tab, click on the "Add Library" button on the right, then select "Server Runtime" in the popup window and click "Next"--"Finish" and "OK" of the parent window. button to successfully introduce the jar package required by the servlet. What do you think? Is there any more error in Cstudentservlet.java?

The next step is to configure the servlet to be self-booting, as long as you add the following to the Web. xml:

  <servlet> <servlet-name>CStudentServlet</servlet-name> <servlet-class> Com.test.servlet.cstudentservlet</servlet-class> <load-on-startup>1</load-on-startup>   </servlet> <servlet-mapping> <servlet-name>CStudentServlet</servlet-name> <url-pattern >/servlet/CStudentServlet</url-pattern>   </servlet-mapping>


Remember to post the site, click the message on Tomcat, select "Add and Remove", and publish the Javawebservice to Tomcat.

The next step is just to publish and launch the site. If you do not, then right-click on the server, select "Publish", and then select "Start" will be OK.

After successful startup, you only need to enter HTTP://192.168.56.1:8082/JAVAWEBSERVICE/STUDENT?WSDL in the browser, you can enter the following information:


All right, it's done!

How to quickly publish the WebService service in Java

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.