1. WebService getting started-helloService, webservice getting started tutorial

Source: Internet
Author: User
Tags webservice annotation

1. WebService getting started-helloService, webservice getting started tutorial

As a beginner, helloworld deserves it ,,,,

 

1. Create two Java projects, one as the webservice server and the other as the webservice client.

2. First, write the service end in the WSService project. Note that the details are already reflected in the Code.

Java code
  • Package com. wang. webservice. service;
  • Import javax. jws. WebService;
  • Import javax. xml. ws. Endpoint;
  • /*
  • * @ WebService Annotation
  • * The server that marks this class as webservice
  • */
  • @ WebService
  • Public class HelloService {
  • /*
  • * A service class must have a method that can be called
  • * This method cannot be static or finally.
  • */
  • Public String say (){
  • Return "hello world !! ";
  • }
  • Public static void main (String [] args ){
  • /*
  • * Parameter 1: service address
  • * Parameter 2: Service Class
  • */
  • Endpoint. publish ("http: // 127.0.0.1: 1234/wsservice/hello", new HelloService ());
  • }
  • }
  • Package com. wang. webservice. service; import javax. jws. webService; import javax. xml. ws. endpoint;/** @ WebService annotation * indicates that this class is a webservice server */@ WebServicepublic class HelloService {/** a service class, there must be a method that can be called * This method cannot be static or finally */public String say () {return "hello world !! ";} Public static void main (String [] args) {/** parameter 1: service address * parameter 2: service class */Endpoint. publish ("http: // 127.0.0.1: 1234/wsservice/hello", new HelloService ());}}

    3. After running the project, as shown in:

     

    3. At this time, the webservice server is ready to be called by the client. To enable the client to use the server, make some preparations. First, we need to know what the wsdl is, and enter the previously defined service address in the browser, as shown in: this is the generated esdl, which is actually the user manual. The usage is reflected in the client code.

    4. In cmd, enter the command: F: \> wsimport-s. http: // 127.0.0.1: 1234/wsservice/hello? Wsdl to generate code that can be used by the client,

     

    The generated file is in drive F:

    The file path is the same as the package name of the service class we wrote.

    5. copy the Code (including the package) to our client code:

    6. Compile the client code:

    Java code
  • Package com. wang. webservice. client;
  • Import com. wang. webservice. service. HelloService;
  • Import com. wang. webservice. service. HelloServiceService;
  • Public class HelloClient {
  • Public static void main (String [] args ){
  • /*
  • * In the WSDL file:
  • * <Service name = "HelloServiceService">
  • * Obtain
  • */
  • HelloServiceService hss = new HelloServiceService ();
  • /*
  • * In the WSDL file:
  • * <Port name = "HelloServicePort" binding = "tns: HelloServicePortBinding">
  • * Obtain
  • */
  • HelloService hs = hss. getHelloServicePort ();
  • String s = hs. say ();
  • System. out. println (s );
  • }
  • }
  • Package com. wang. webservice. client; import com. wang. webservice. service. helloService; import com. wang. webservice. service. helloServiceService; public class HelloClient {public static void main (String [] args) {/** In the WSDL file: * <service name = "HelloServiceService"> * Get */HelloServiceService hss = new HelloServiceService ();/** In the WSDL file: * <port name = "HelloServicePort" binding = "tns: HelloServicePortBinding"> * obtain */HelloService hs = hss. getHelloServicePort (); String s = hs. say (); System. out. println (s );}}

    7. Run the client program:

    Java enterprise-level general permission security framework source code SpringMVC mybatis or hibernate + ehcache shiro druid bootstrap HTML5

    [Download java framework source code]

    Related Article

    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.