Develop Webservice Based on JAX-WS on MyEclipse

JAX-WS (Java API for XML-Web Services)

environment description:
MyEclpose 6.5 blue Milestone-1
jdk 1.6.0_15
tomcat 6.0

Create and publish server-side applications

1. Create a new web service project.
The settings are as shown.


 
2. Create an implementation class, the code is as follows:

package net;

public class addImpl { public
    double add(double num1,double num2){     return
        num1+num2        ; ps: Web services can be implemented by a single Java class, but it is best to use an "interface + implementation" approach





3. Create a web service
, select the project name, and click the new web service button on the toolbar.


 
Then configure as follows:

 
Then (enter the name of the implementation class in JAVA BEAN, select generate wsdl in project):


After clicking finish, there is an additional proxy class addImplDelegate.java for the implementation class in the

package. 4. Import the package
project-property-build path-add library-myeclipse libraries and import the following two packages:


If these two packages are missing, tomcat starts with the following error:

FATAL: Error configuring application listener of class com.sun.xml.ws.transport.http.servlet.WSServletContextListener
java.lang.ClassNotFoundException: com.sun.xml. ws.transport.http.servlet.WSServletContextListener

5. Start tomcat, verify and obtain the WSDL file

After starting tomcat, enter http://localhost:8080/jaxws_src/addImplPort in the browser and click the link in the page to view the WSDL file, if the The file can be displayed correctly to prove that the server side is done.
The uri of the wsdl file is http://localhost:8080/jaxws_src/addImplPort?wsdl (you may ask where the addImplPort in the URI comes from, in fact, the name is set by the Services port in Figure 4)

ps: in web. There are the following classes in xml, you can see the function

code of this /addImplPort
<servlet>
      <description>JAX-WS endpoint - addImplService</description>
      <display-name>addImplService</display-name>
      <servlet-name>addImplService </servlet-name>
      <servlet-class>
          com.sun.xml.ws.transport.http.servlet.WSServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>addImplService</servlet-name>
      <url-pattern>/addImplPort</url-pattern>
  </servlet-mapping>

 


Create a client call:

(Don't close TOMCAT, otherwise...)
1. Create a new java project and create a package (package name security requirements) net.


2. Select the package point new web services client (refer to the second picture in this article)
   ps: Different from xfire, the client is not necessarily the same type of web services client as the server, it can be an ordinary project and OK!!


3. Specify the URI of the wsdl or the file path of the wsdl (see above).
The program generates some classes in the net package as shown in the figure:


 
ps: add.java in the figure is a javabean, you should know what it is useful for if you are smart! !


4. Create a new test class:

package com;

import net.AddImplDelegate;
import net.AddImplService;

public class test {   
    public static void main(String[] args) {
        AddImplService service=new AddImplService();
        AddImplDelegate d= service.getAddImplPort( );
        double result= d.add(15.6, 12.5);
        System.out.println(result);
    }
}


ok! You're done! !

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326984350&siteId=291194637