将Web服务添加到已有的Java项目中 [英] Add a web service to a already available Java project

查看:110
本文介绍了将Web服务添加到已有的Java项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手。我有一个Java项目。它在我的Windows 7机器上运行完美。我想将此项目的一些功能用作Web服务,以便能够在我的Silverlight应用程序中使用它们。 Silverlight应用程序和此Java项目都将位于单台服务器计算机上。我遇到的问题是,当我右键单击项目时,新菜单中没有Web服务。如何在项目中添加Web服务?谢谢。

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.

推荐答案

根据我在上面评论中链接的文章:: 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.

定义Web服务的示例:

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";
    }

}

注意 @ WebService 和 @ 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 来访问您的Web服务。此时,您可以轻松配置要在应用程序之间来回发送的内容。

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.

这篇关于将Web服务添加到已有的Java项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆