2011-05-31 24 views
0

我不知道如何讓參數化的@PATH工作。參數化的REST @Path

這裏是我的web.xml

<servlet-mapping> 
    <servlet-name>JerseyServlet</servlet-name> 
    <url-pattern>/ND/*</url-pattern> 
</servlet-mapping> 

這裏是我的資源類:

@Path("/ND") 
public class TransactionResource 
{ 
@Context UriInfo uriInfo; 

public TransactionResource() 
{ 
} 

@GET 
@Produces(MediaType.TEXT_PLAIN) 
public String itWorks() 
{ 
    return String.format("Get is OK. %s", DateUtil.now()); 
} 

@GET @Path("/NJ") 
@Produces(MediaType.TEXT_PLAIN) 
public String itWorksForState() 
{ 
    return String.format("Get is OK for NJ. %s", DateUtil.now()); 
} 

@POST 
@Produces(MediaType.TEXT_PLAIN) 
@Consumes(MediaType.APPLICATION_XML) 
public String addTransaction(Transaction pTransaction) throws Exception 
{ 
    //some code here   
    return "Successful Transmission"; 
} 

當我在URL http://my_web_app:8080/ND做一個GET或POST那麼這兩種方法很好地工作。 但由於某些原因,URL http://my_web_app:8080/ND/NJ上的GET方法始終返回404-NotFound。

我在這裏做了什麼錯?

感謝

回答

0

你有4級路徑:

  1. 你的web應用語境在服務器的路徑:也許MYAPP
  2. 的JAX-RS的servlet的路徑在web.xml中:Here/ND /,但我建議/ws
  3. Resource的路徑:第一個@Pat h在班級之上。你應該有@Path("transaction")
  4. 然後在每個方法上面有一個可選的@Path。假設你不用任何方法添加任何@Path。

現在你有 @Path( 「交易」) 公共類TransactionResource {

@GET 
    @Produces(MediaType.TEXT_PLAIN) 
    public String itWorksForState() 
    { 
     return String.format("Get is OK for REST. %s", DateUtil.now()); 
    } 
} 

轉到Firefox和類型 http://my_web_app:8080/myapp/ws/transaction:你應該閱讀日期

要是你加

@Path("morepath") 
    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    public String itWorksForState() 
    { 
     return String.format("Get is OK for REST. %s", DateUtil.now()); 
    } 

然後你必須去http://my_web_app:8080/myapp/ws/transaction/morepath