2016-07-08 60 views
0

我們可以有@Path註釋的空基路徑嗎? 即。 @Path( 「/」)我們可以有一個空的基地路徑?

我想提供REST API http://servername/abc

@Path("") 
public class YourResource { 

    @Path("/abc") 
    @GET 
    public Responce method1(){ 
    return Response.ok("ok").build(); 
    } 

} 

當我做到這一點,則拋出異常

javax.ws.rs.WebApplicationException 在的com.sun。 jersey.server.impl.uri.rules.TerminatingRule.accept(TerminatingRule.java:66) 在com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 在COM。 sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)

UPDATE - 我在GuiceServletContextListener結合像下面

綁定(YourResource.class)。在(Singleton.class); (GuiceContainer.class);服務(「/ abc」)。從類

+1

當然。讓基本路徑完全空着,你會得到你想要的。 – Christian13467

+0

我試過了@Path(「」),並且仍然在 –

回答

0

刪除@Path("/")註釋完全

前綴斜槓(/)到方法級別路徑註解,像@Path("/abc")

請驗證您的URI是否如下所示映射到java方法。

雖然啓動服務器,你可以看到的URI的映射到Java方法,在Eclipse控制檯這樣的事情...

Mapped "{[/static/transit],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.List<com.xxx.yyy.zzz.domain.Transit> com.xx.yy.zz.controller.abcDataController.getAllTransit()

======== ================================================== ========

UPDATE:

根據在下面的java doc中,有這兩個例子之間沒有什麼區別,兩者都可以。 要麼你可以使用領先斜線與的URI或不使用任何領導斜線與的URI,都是相同。

路徑是相對的。對於註釋類,基本URI是 應用程序路徑,請參閱ApplicationPath。對於帶註釋的方法,基本URI是包含類的有效URI。對於 目的絕對值化靠基部URI,在一個路徑中的領先「/」 的路徑的被忽略,並且,就好像它們在 「/」結束鹼的URI進行處理。

@Path("message") 

public class MessageServices { 

    @PUT 
    @Path("sendsms") 
    @Consumes(MediaType.APPLICATION_JSON) 
    @Produces({MediaType.APPLICATION_JSON}) 
    public Response sendSms() { 
     //.... 
    } 

} 

OR

@Path("/message") 
public class MessageServices { 

    @PUT 
    @Path("/sendsms") 
    @Consumes(MediaType.APPLICATION_JSON) 
    @Produces({MediaType.APPLICATION_JSON}) 
    public Response sendSms() { 
     //.... 
    } 

} 
+0

以上得到了同樣的錯誤我試過了。我得到這個例外。 「com.sun.jersey.api.NotFoundException:null for uri:http:// localhost:8080/abc /」 –

+0

你能發佈你的最新代碼嗎? –

+0

您可以在類級別嘗試像@Path(「/ test」)和方法級別的@Path(「/ abc」)?並且在類級別使用uri:localhost:8080/test/abc –

相關問題