且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 Web 服务添加到已经可用的 Java 项目

更新时间:2023-02-17 07:40:53

基于我在上面评论中链接的文章 :: http://www.ibm.com/developerworks/webservices/tutorials/ws-eclipse-javase1/index.html

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

使用 JWS 注释,您可以在 Java 应用程序中设置 Web 服务以公开其某些功能.不需要额外的库.下面的例子是用 Java 6 编写的.

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.

定义网络服务的示例:

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class MyWebService {

    @WebMethod
    public String myMethod(){
        return "Hello World";
    }

}

注意 @WebService的 2 个注释> 和 @WebMethod.阅读链接的 API 并根据需要配置它们.这个例子可以在不改变任何东西的情况下工作

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

然后您只需要设置监听器.您会在 javax.xml 类中找到它.ws.Endpoint

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);

    }
}

运行此程序,您将能够使用 http://127.0.0.1 访问您的网络服务:8023/_WebServiceDemo?WSDL.此时,可以轻松配置要在应用程序之间来回发送的内容.

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.

如您所见,无需设置特殊的 Web 服务项目供您使用.

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