Add a web service to a already available Java project

半世苍凉 提交于 2020-02-09 04:41:04

问题


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 use them in my Silverlight app. Both the Silverlight app and this Java project would be on the single server machine. The problem I have is that when I right click on the project there's no Web Service in the New menu. What should I do to add a web service to my project? Thanks.


回答1:


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.



来源:https://stackoverflow.com/questions/5595028/add-a-web-service-to-a-already-available-java-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!