开发者

Use Post method of jersey-Rest and gets params from url

I want to develop rest api.such as: http://localhost:8080/TestSomeWay/resources/test/create?meg=sadasd&name=sadasd and get params from urlparams exp."meg"&"name" I am using jersey to develop a Restful post method it dose not make it out code:

@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
@Override
public String create( @FormParam("meg")String megString, @FormParam("name")String nameString) {
   TestUser testUser=new TestUser();
   testUser.setMeg(megString);
   testUser.setName(nameString);
   em.persist(testUser);
   em.flush();开发者_如何学编程
   return testUser.getId().toString();

}


You seem to be confused as to what you are trying to achieve, and that's showing up as an incoherent API. Once you've gone wrong that way, it's small wonder that things are going wrong!

First off, you've got to figure out whether you're using GET, PUT or POST, and in the latter two cases, what is the content type (or types) that you are consuming, as both PUT and POST are typically dealing with a document incoming. Moreover, if you're doing anything that isn't idempotent (i.e., so that it would be “the same” if you did it twice in a row as if once) then you should definitely be using POST; the classic example is paying for some goods, which you definitely don't want to do twice, whereas setting your preferences can be idempotent. The final complication is that it is usually bad style to mix query parameters with a body; either the parameters are in the query part or they are in the body (or they are in the path, but in that case you're dealing with different resources conceptually).

If you're just dealing with HTML forms, the two styles of method you'll want will be like this:

@GET
@Path("/create")
@Produces(MediaType.TEXT_PLAIN)
public String createFromGet(
        @QueryParam("meg") String meg,
        @QueryParam("name") String name) {
    ...
    return theString;
}
@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public Response createFromPost(
        @FormParam("meg") String meg,
        @FormParam("name") String name) {
    ...
    return Response.created(theNewUrl).entity(theString).build();
}

The first deals with a GET on a URL like /create?meg=foo&name=bar and the second deals with a POST to a URL like /create. However, given the name “create” I'd be tempted to go with just using the POST version and to not try to support encoding the parameters in the query part; creation is one of those things that tends to not be idempotent.

Note that I have assumed that your creation is making a resource (that's good RESTful programming!) so I adjusted to return the right kind of response; it's a bit more involved than the usual, but is exactly the right thing.


You should QueryParam instead of FormParam to obtain the functionality you want

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜