java web service base version

First write the Service

package service;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
 * A simple webService demo class, use the  @WebService annotation to declare that this is a webService class
 */

@WebService
public class JwsServiceDemo {

    /**
     * The method in the WebService annotation will be published to the service
     * @param Name
     * @return
     */
    public String helloJws(String userName) {
        return " Hello dear " + userName;
    }
    
    public String helloEverybody(String userName) {
        return " Hello dear " + userName;
    }
    
    /**
     * The static method will not be posted to the webService 
     * @param Name
     * @return
     */
    public static String helloJwsStatic(String Name) {
        return " Hello dear " + Name;
    }
    
    /* *
     * When adding the WebMethod annotation with exclude and the attribute equal to true, it will not be published to the service
     * @param Name
     * @return
     */
    @WebMethod(exclude=true)
    public String hellJwsMethod(String Name) {
        return " Hello dear " + Name;
    }
    
    public static void main(String[] args) {
        
        /*
         * Call javax. publish in xml.ws.EndPoint to publish a service
         * parameter service publishing address, port
         * class providing service 
         */
        Endpoint.publish("http://localhost:8084/Service", new JwsServiceDemo());
        System .out.println("WebServie service started successfully~~~");
        
    }
}

    After the startup is successful, you can enter http://localhost:8084/Service?wsdl in the browser. Notice? is in English

    If the startup is correct, you can see the configuration file of Service.xml, in which you can see the methods, parameters and other class content in the webService

    Then download the class in the webService in the system using the java command in the command line

    wsimport -keep -p com.deom.webService http://localhost:8084/Service?wsdl

    The detailed class content in wsimport can be viewed with wsimport -help

    When exporting, pay attention to write the package name, namely com.deom.webService, to ensure that the package name will not be modified when the .java file is imported into the compiler. If the package name is modified, errors may occur.

    After writing the webService service class and exporting the specific server code file, you can call the webService port of the server.

      

import com.deom.webService.JwsServiceDemo;
import com.deom.webService.JwsServiceDemoService;

/**
 * webSerivce client simple case
 *
 */
public class DemoWebCient {

    public static void main(String[] args) {
        
        /*
         * new a webService service
         * Use the port of the service to get the appropriate method, each method in the webService will generate a corresponding method class
         * Through this method class, Call the method in webService
         */
        JwsServiceDemoService jws = new JwsServiceDemoService();
        JwsServiceDemo hello = jws.getJwsServiceDemoPort();
        System.out.println(hello.helloJws("Bob"));
        
    }
    
}

Complete the above operation and print out Hello dear Bob in the control band Complete the operation of a webService client calling the server


 

Guess you like

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