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 :
- The path of your web application Context in the server : Maybe myapp
- The path of the Jax-Rs servlet in web.xml : Here /ND/, but I would suggest /ws
- The path of the Resource : The first @Path above the class. You should probably have
@Path("transaction")
- 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
精彩评论