开发者

Parameterized REST @Path

I am at loss on how to get the parameterized @PATH to work.

Here is my web.xml

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

Here is my resource class:

@Path("/ND")
public class 开发者_StackOverflowTransactionResource 
{
@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";
}

When I do a GET or POST at the URL http://my_web_app:8080/ND then both methods work fine. But for some reasons, the GET method at URL http://my_web_app:8080/ND/NJ always return the 404-NotFound.

What have I done wrong here?

Thanks


You have 4 level of path :

  1. The path of your web application Context in the server : Maybe myapp
  2. The path of the Jax-Rs servlet in web.xml : Here /ND/, but I would suggest /ws
  3. The path of the Resource : The first @Path above the class. You should probably have @Path("transaction")
  4. Then an optional @Path above each method. Let's say that you dont add any @Path in any method.

Now you have @Path("transaction") public class TransactionResource {

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

Go to Firefox and type http://my_web_app:8080/myapp/ws/transaction : you should read the Date

If ever you add

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

Then you must go to http://my_web_app:8080/myapp/ws/transaction/morepath

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜