Add a web service to a already available Java project

前端 未结 1 1715
旧巷少年郎
旧巷少年郎 2020-11-29 11:58

I\'m new to Java. I have a Java project. It runs perfectly on my Windows 7 machine. I want to use some of the functionalities of this project as Web Services to be able to u

相关标签:
1条回答
  • 2020-11-29 12:16

    Based on the article I linked in the comments above :: http://www.ibm.com/developerworks/webservices/tutorials/ws-eclipse-javase1/index.html

    With the JWS annotations you can setup a Web Service in your java application to expose some of its functionality. There is no extra libraries needed. The below examples were written with Java 6.

    An example of Defining your web service :

    import javax.jws.WebMethod;
    import javax.jws.WebService;
    
    @WebService
    public class MyWebService {
    
        @WebMethod
        public String myMethod(){
            return "Hello World";
        }
    
    }
    

    Note the 2 annotations of @WebService and @WebMethod. Read their API's which are linked and configure them as required. This example will work without changing a thing

    You then only need to setup the Listener. You will find that in the class javax.xml.ws.Endpoint

    import javax.xml.ws.Endpoint;
    
    public class Driver {
    
        public static void main(String[] args) {
            String address = "http://127.0.0.1:8023/_WebServiceDemo";
            Endpoint.publish(address, new MyWebService());
            System.out.println("Listening: " + address);
    
        }
    }
    

    Run this program and you will be able to hit your web service using http://127.0.0.1:8023/_WebServiceDemo?WSDL. At this point it is easy to configure what you want to send back and forth between the applications.

    As you can see there is no need to setup a special web service project for your use.

    0 讨论(0)
提交回复
热议问题