2016-03-18 44 views
0

我有一個wadl文件,我通過該文件爲REST服務生成對象和接口,並使用wadl2java工具生成對象和接口。我有這些接口的實現,到目前爲止都很好。我也配置了servletweb.xml,但我不能讓這個工作。現在我的網站的XML看起來是這樣的:將生成的REST服務接口實現綁定到servlet

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

    <servlet> 
     <servlet-name>MyServlet</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>jersey.config.server.provider.classnames</param-name> 
      <param-value>com.foo.ws.RestServiceImpl</param-value>    
     </init-param>  
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>MyServlet</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

生成的REST接口看起來像以下:

package com.foo.schemas; 

import javax.ws.rs.Consumes; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import com.foo.schemas.BarRequest; 
import com.foo.schemas.BarResponse; 
import com.foo.schemas.FooRequest; 
import com.foo.schemas.FooResponse; 

@Path("services") 
public interface RestService { 

    @POST 
    @Consumes("application/xml") 
    @Produces("application/xml") 
    BarResponse getBar(BarRequest barRequest); 

    @POST 
    @Consumes("application/xml") 
    @Produces("application/xml") 
    FooResponse getFoo(FooRequest fooRequest); 
} 

而且它的實現:

package com.foo.ws; 

import org.glassfish.jersey.server.ResourceConfig; 
import com.foo.RestService; 
import com.foo.schemas.BarRequest; 
import com.foo.schemas.BarResponse; 
import com.foo.schemas.FooRequest; 
import com.foo.schemas.FooResponse; 

public class RestServiceImpl extends ResourceConfig implements RestService { 
    public RestServiceImpl() { 
     register(new AbstractBinder() { 
      @Override 
      protected void configure() { 
       bind(RestServiceImpl.class).to(RestService.class); 
      } 
     }); 
    } 

    @Override 
    BarResponse getBar(BarRequest barRequest) { 
     // Implementation omitted... 
    } 

    @Override 
    FooResponse getFoo(FooRequest FooRequest) { 
     // Implementation omitted... 
    } 
} 

我想,這看起來不錯,但是當我啓動我獲得的Web服務HTTP ERROR 503

Problem accessing /my-web-service/. Reason: 

org.glassfish.jersey.server.model.ModelValidationException: Validation of  
the application resource model has failed during application initialization. 
[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP 
method POST and input mime-types as defined by"@Consumes" and "@Produces" 
annotations at Java methods public com.foo.schemas.BarResponse 
com.foo.ws.RestServiceImpl.getBar(com.foo.schemas.BarRequest) and public 
com.foo.schemas.FooResponse 
com.foo.ws.RestServiceImpl.getFoo(com.foo.schemas.FooRequest) at matching 
regular expression /services. These two methods produces and consumes 
exactly the same mime-types and therefore their invocation as a resource 
methods will always fail.; 
source='[email protected]' 

它看起來像接口和實現之間的綁定是正確的,但我不明白爲什麼我得到這個錯誤?是的,對於@Consumes@Produces註釋都使用相同的MIME-types定義方法,但是這是如何生成界面的wadl2java?我不知道如何解決這個問題,servlet或wadl2java工具是否有配置錯誤?我沒有任何線索,任何幫助都非常讚賞,因爲我堅持這一點。

編輯 - 添加WADL文件

<?xml version="1.0" encoding="UTF-8"?> 
<application xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:foo="http://schemas.foo.com" 
     xsi:schemaLocation="http://wadl.dev.java.net/2009/02 http://www.w3.org/Submission/wadl/wadl.xsd" 
     xmlns="http://wadl.dev.java.net/2009/02"> 
<doc xml:lang="en">This wadl describes....</doc> 
<grammars> 
    <include href="foo.xsd"/> 
</grammars> 
<resources base="/foo"> 
    <resource path="services"> 
     <method id="getBar" name="POST"> 
      <doc xml:lang="en">Get all bars.</doc> 
      <request> 
       <representation mediaType="application/xml"> 
        <param required="true" style="plain" id="barRequest" name="barRequest" type="foo:BarRequest"/> 
       </representation> 
      </request> 
      <response status="200"> 
       <representation mediaType="application/xml"> 
        <param required="true" style="plain" id="barResponse" name="barResponse" type="foo:BarResponse"/> 
       </representation> 
      </response> 
     </method> 
     <method id="getFoo" name="POST"> 
      <doc xml:lang="en">Get all foos.</doc> 
      <request> 
       <representation mediaType="application/xml"> 
        <param required="true" style="plain" id="fooRequest" name="fooRequest" type="foo:FooRequest"/> 
       </representation> 
      </request> 
      <response status="200"> 
       <representation mediaType="application/xml"> 
        <param required="true" style="plain" id="fooResponse" name="fooResponse" type="foo:FooResponse"/> 
       </representation> 
      </response> 
     </method> 
    </resource> 
</resources> 

回答

1

的問題是w.r.t的資源是如何暴露出來。

  1. 有2 POST方法的暴露,無論是聽路徑 「/服務」。
  2. 這兩種方法消耗和生產相同的MimeType

問題 - 是否該資源被擊中。然後澤西不會理解爲了得到結果而調用哪個API。 它是getBar()或getFoo()

現在,更改取決於期望是什麼(根據需要需要調用API時需要調用哪個&)。 您必須相應地修改WADL以確保每個資源都偵聽唯一路徑/消耗/生產類型。

@Path("services") 
public interface RestService { 

    @POST 
    @Consumes("application/xml") 
    @Produces("application/xml") 
    BarResponse getBar(BarRequest barRequest); 

    @POST 
    @Consumes("application/xml") 
    @Produces("application/xml") 
    FooResponse getFoo(FooRequest fooRequest); 
} 
+0

Okey,我明白你給他們的問題和解釋。我現在也通過刪除除了一個之外的所有方法進行了測試,並使用它自己的路徑對每個方法進行了註釋,並且這兩種方案都可以工作。然後我覺得奇怪的是,我已經從一些顧問那裏得到了這個WADL文件,它應該準備好使用,但正如你所描述的那樣,以及我認爲這個WADL不完全完整,因爲wadl2java產生這樣的接口。這是一個正確的假設嗎? – Robert

+0

@Robert是的,沒錯。 WADL文件似乎是錯誤的。是否有可能只從WADL文件中上傳代碼中的這部分代碼?另外,我不認爲WADL解析器驗證/解析文檔會將其視爲錯誤。因爲對於WADL來說,所需要的只是當前資源能夠提供什麼,而不是檢查可能提供的其他資源以及它們是否相互衝突。在任何情況下,WADL文件都需要更新。 –

+0

我現在將WADL文件添加到問題中。還有一件事我真的不明白。如果WADL文件是錯誤的,那麼它在SoapUI中的導入如何順利進行,並且不會遇到路徑等問題? – Robert