开发者

Add a web service to a already available Java project

开发者 https://www.devze.com 2023-02-22 20:07 出处:网络
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 a

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消