2012-02-26 15 views
4

我試圖在eclipse中創建一個簡單的Web服務。首先,我創建了一個空的java項目和src文件夾中在Eclipse中創建簡單的JAX-WS WebService

  1. 加入下列三個文件Greeting.java
package com.alfaisaliah; 

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

@WebService 
public interface Greeting { 
    @WebMethod 
    String sayHello(String name); 
} 
  • 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; 
        } 
    } 
    
  • 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()); 
        } 
    } 
    

    我遵循的教程沒有指定任何服務器來運行Web服務!我想知道是否需要指定任何服務器。我已經有了Tomcat v5.5,但在本例中沒有使用它。每當我將這個項目作爲一個java項目運行時,我都會遇到某種錯誤。任何人都可以請幫助我確定我的問題在哪裏試圖運行Web服務。這裏是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 
    

    同樣的輸出,當我再次運行該項目它說,該地址已在使用

    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 
    

    我會感激你的幫助球員:)

    +0

    關於創建包裝bean的消息是OK的。它基本上是JAX-WS即時生成WSDL的能力的一部分。 – 2012-02-27 15:54:32

    回答

    4

    The tutorial i'm following doesn't specify any server to run the web service on! I'm wondering if I need to specify any server.

    您不需要帶有此代碼的服務器。
    main在:

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

    開始引擎蓋下光HTTP服務器(可JKD 1.6後),並將其部署Web服務處理所有流入/流出流量。

    這裏的問題是,你錯過了一個步驟:
    你必須使用wsgen工具(在Java中可用)生成所需的工件。

    檢查出在這裏:JAX WS tutorial
    wsgen -d build -s build -classpath build helloservice.endpoint.Hello
    和閱讀有關wsgen

    說實話,我不記得你是如何通過Eclipse做(其實我不知道這是否可以在Eclipse 自動工作,沒有需要運行wsgen自己),但你可以手動運行它,只需將生成的工件複製到您的項目中即可。

    對於

    Server Runtime Error: java.net.BindException: Address already in use

    這是不言自明的:只需使用其他端口。 8081已被使用。

    +0

    感謝您解決這個問題。現在我知道問題不在代碼或服務器中。我查看了wsgen命令工具,並瞭解了大部分選項和參數;但是,當我cd到項目文件夾和wsgen它說SEI失蹤!!!我不知道如果我應該cd到項目的src文件夾或bin文件夾中,然後應用wsgen?那裏的大多數教程都沒有詳細說明,他們只是使用命令和選項。謝謝 – moeabdol 2012-02-26 11:44:12

    +0

    好的...我發現如何使用wsgen生成工件和wsdl ...我也將端口更改爲8088.我應該將這些文件放在項目層次結構中? – moeabdol 2012-02-26 12:45:40

    +1

    在其相應的包裝下 – Cratylus 2012-02-26 14:57:00