Lightweight WebService -- JAX-WS framework for the use of real (JWS) released We

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

WebService has always been very popular, especially in the Java camp, emerge in an endless stream of WebService framework and technology. The famous XFile (New CXF), Axis1, Axis2 etc.

  And Sun company also unwillings to lag behind, from the early JAX-RPC to the now mature, support RPC calls and message passing JAX-WS through the test of the market, very mature, and the use of JAX-WS development of WebService gains is very big, it is lightweight.

  

  We use JAX-WS to develop WebService requires only a few steps are simple: write interface and implementation of => => build a client (or use).

  While in the stage of development we don't need to import external package jar, because the API is ready. First is the interface (interface requires only the class marked as @WebService, to be exposed to the client method marked @WebMethod can rest, such as @WebResult, @WebParam are not necessary, and the client and server communications using RPC and Message-Oriented two, difference and configuration later):

package service;

import java.util.Date;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/**
 * As the WebService interface test
 * 
 * @author Johness
 * 
 */
@WebService
public interface SayHiService {

    /**
     * Implementation of WebService test method
     */
    @WebMethod
    void SayHiDefault();

    /**
     * Implementation of WebService test method (.)
     * 
     * @param name
     */
    @WebMethod
    void SayHi(@WebParam(name = "name") String name);

    /**
     * Implementation of WebService test method (for time check)
     * 
     * @Param clentTime client time
     * @Return 0 represents time verification failed 1 indicates successful calibration
     */
    @WebMethod
    @WebResult(name = "valid")
    int CheckTime(@WebParam(name = "clientTime") Date clientTime);
}

 Then the implementation class (note @WebService and its endpointInterface property is necessary):

package serivce.imp;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jws.WebService;

import serivce.SayHiService;

/**
 * As a test of the WebService implementation class
 * 
 * @author Johness
 * 
 */
@WebService(endpointInterface = "serivce.SayHiService")
public class SayHiServiceImp implements SayHiService {

    @Override
    public void SayHiDefault() {
        System.out.println("Hi, Johness!");
    }

    @Override
    public void SayHi(String name) {
        System.out.println("Hi, " + name + "!");
    }

    @Override
    public int CheckTime(Date clientTime) {
        // Accurate to seconds
        String dateServer = new java.sql.Date(System.currentTimeMillis())
                .toString()
                + " "
                + new java.sql.Time(System.currentTimeMillis());
        String dateClient = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .format(clientTime);
        return dateServer.equals(dateClient) ? 1 : 0;
    }

}

 Then release (there are two general ways):

  One way (this way only as a debugging, have the following bug:

    jdk1.6u17?The compiler does not support document mode of soap in Endpoint.publish, must be in the service interfaces and implementation classes to add a “ @SOAPBinding (style = SOAPBinding.Style.RPC) ” note,

    Limited access, seemed to be the machine access (should be bound to the publish URL, the localhost can only access the machine)……):

package main;

import javax.xml.ws.Endpoint;

import serivce.imp.SayHiServiceImp;

public class Main {

    /**
     * Release WebService
     * Simple
     */
    public static void main(String[] args) {
        Endpoint.publish("http://localhost/sayhi/service/sayHi", new SayHiServiceImp());
    }

}

 Mode two (web server based on Servlet mode):

  In the case of Tomcat, the first to write sun-jaxws.xml file and under the WEB-INF:

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

 Then change to web.xml, the addition of listener and servlet (url-pattern to be the same oh):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    
    <listener>  
        <listener-class>
            com.sun.xml.ws.transport.http.servlet.WSServletContextListener  
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>SayHiService</servlet-name>  
        <servlet-class>
            com.sun.xml.ws.transport.http.servlet.WSServlet  
        </servlet-class>
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SayHiService</servlet-name>  
        <url-pattern>/service/sayHi</url-pattern>  
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 Finally, deployed to the Tomcat, it is worth mentioning that you may need to add the following jar package (because there is no Tomcat):

            

  Start Tomcat.


  After deploying the open browser enter the URL: (if you are using Tomcat, don't forget to add the port), can see that the successful launch.


  The last is the client use, because WebService is a platform and language independent XML based, so we can use different language to write or to generate client. The following is the use of JDK in the Windows platform tools wsimport generated client mode:

  The wsimport tool to generate JDK own, above my client file to the desktop SRC file (-d), and retains the source file (-keep), specify a package name(-p).


  Then we can use the generated file to invoke the server exposed.:

            

    It is worth mentioning that you are generated using JDK and JRE supporting the needs of your client!

  We can find from the directory structure above: each webmethod server are separate parsing becomes a class (if you use entity, entity will be resolved to the client, and is the source code, it is recommended to use entity carefully).

  

  Our service is to generate a proxy class to invoke the service, then we have a look the use:

package main;

import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.ahe.webservice.client.SayHiService;
import org.ahe.webservice.client.SayHiServiceImpService;

public class Main {

    /**
     * Test
     * @throws DatatypeConfigurationException 
     */
    public static void main(String[] args) throws DatatypeConfigurationException {
        // Access to service
        SayHiService service = new SayHiServiceImpService().getSayHiServiceImpPort();
        
        // sayhi
        service.sayHiDefault();
        service.sayHi("Ahe");
        
        // checktime
        // Here mainly to talk time and date of the XML transfer, methods are also more complicated
        GregorianCalendar calender = new GregorianCalendar();
        calender.setTime(new Date(System.currentTimeMillis()));
        XMLGregorianCalendar xmldate = DatatypeFactory.newInstance().newXMLGregorianCalendar(calender);
        System.out.println(service.checkTime(xmldate));
    }

}

 Have a look the server output, whether we call success:

        

    

    Success!

    For the client to check the time also receive feedback.:

        


The original address

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lori at November 18, 2013 - 10:30 AM