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

查看:31
本文介绍了将 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.

定义网络服务的示例:

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.此时,可以轻松配置要在应用程序之间来回发送的内容.

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天全站免登陆