简体   繁体   English

在Eclipse中创建一个简单的JAX-WS WebService

[英]Creating a Simple JAX-WS WebService in Eclipse

I'm trying to create a simple web service in eclipse. 我正在尝试在eclipse中创建一个简单的Web服务。 First i created an empty java project and added the three following files in the src folder 首先,我创建了一个空的java项目,并在src文件夹中添加了以下三个文件

  1. Greeting.java Greeting.java
package com.alfaisaliah;

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

@WebService
public interface Greeting {
    @WebMethod
    String sayHello(String name);
}
  1. GreetingImp.java GreetingImp.java
package com.alfaisaliah;

import javax.jws.WebService;

@WebService(endpointInterface="com.alfaisaliah.Greeting")
public class GreetingImp implements Greeting {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}
  1. WSPublisher WSPublisher
package com.alfaisaliah;

import javax.xml.ws.Endpoint;

public class WSPublisher {
    public static void main(String[] args){
        Endpoint.publish("http://localhost:8081/WS/Greeting", new GreetingImp());
    }
}

The tutorial I'm following doesn't specify any server to run the web service on! 我正在遵循的教程没有指定任何服务器来运行Web服务! I'm wondering if I need to specify any server. 我想知道我是否需要指定任何服务器。 I already have Tomcat v5.5 but am not using it in this example. 我已经有Tomcat v5.5但是在这个例子中没有使用它。 Whenever I run this project as a java project I get some kind of error. 每当我将这个项目作为一个java项目运行时,我都会遇到一些错误。 Can anyone please help me identify where my problem is trying to run the web service. 任何人都可以帮我确定我的问题是在尝试运行Web服务。 Here is the output of the eclipse console 这是eclipse控制台的输出

Feb 26, 2012 12:01:00 PM com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
INFO: Dynamically creating request wrapper Class com.alfaisaliah.jaxws.SayHello

Feb 26, 2012 12:01:00 PM com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
INFO: Dynamically creating response wrapper bean Class com.alfaisaliah.jaxws.SayHelloResponse

Also when I run the project again it says that the address is already in use 此外,当我再次运行该项目时,它表示该地址已被使用

Feb 26, 2012 12:01:00 PM com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
INFO: Dynamically creating request wrapper Class com.alfaisaliah.jaxws.SayHello

Feb 26, 2012 12:01:00 PM com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
INFO: Dynamically creating response wrapper bean Class com.alfaisaliah.jaxws.SayHelloResponse
Exception in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use

I would appreciate your help guys :) 我很感激你的帮助:)

The tutorial i'm following doesn't specify any server to run the web service on! 我正在关注的教程没有指定任何服务器来运行Web服务! I'm wondering if I need to specify any server. 我想知道我是否需要指定任何服务器。

You don't need a server with this code. 您不需要具有此代码的服务器。
Your main in: 你的main在:

Endpoint.publish("http://localhost:8081/WS/Greeting", new GreetingImp());  

starts a light http server under the hood (available after JKD 1.6) and it deploys your web service handling all incoming/outgoing traffic. 启动一个轻型http服务器(在JKD 1.6之后可用)并部署您的Web服务处理所有传入/传出流量。

The problem here is that you missed a step: 这里的问题是你错过了一个步骤:
You have to generate the required artifacts using the wsgen tool (available in java). 您必须使用wsgen工具(在java中提供)生成所需的工件。

Check out here: JAX WS tutorial for 点击这里: JAX WS教程
wsgen -d build -s build -classpath build helloservice.endpoint.Hello
and read about wsgen . 并阅读有关wsgen

To be honest I don't remember how you do it via Eclipse (actually I am not sure if this can work in Eclipse automatically without you needing to run wsgen yourself) but you can run it manually and just copy the generated artifacts in your project. 说实话,我不记得你是如何通过Eclipse(其实我不知道这是否可以在Eclipse工作自动没有需要运行做wsgen自己),但你可以手动运行它,只是复制生成的工件在您的项目。

As for the 至于

Server Runtime Error: java.net.BindException: Address already in use 服务器运行时错误:java.net.BindException:地址已在使用中

This is self-explanatory: Just use another port. 这是不言自明的:只需使用另一个端口。 8081 is already used. 已经使用了8081。

check this link out, 检查这个链接,

http://www.myeclipseide.com/documentation/quickstarts/webservices_jaxws/ http://www.myeclipseide.com/documentation/quickstarts/webservices_jaxws/

Above link gives step-by-step details for generating both web-service server and client. 以上链接提供了生成Web服务服务器和客户端的分步详细信息。

You start with POJO, no annotation needed, JAX-WS runtime will take care after deployment on Tomcat server. 您从POJO开始,不需要注释,JAX-WS运行时将在Tomcat服务器上部署后注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM