java 使用 jersey-Rest 的 Post 方法并从 url 获取参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7776283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:23:01  来源:igfitidea点击:

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

javarestjersey

提问by user996505

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

我想开发rest api.例如: http://localhost:8080/TestSomeWay/resources/test/create?meg=sadasd&name=sadasd并从urlparams exp."meg"&"name"获取参数我正在使用球衣开发一个 Restful post 方法,它不会把它弄出来代码:

@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();

}

回答by Donal Fellows

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!

您似乎对要实现的目标感到困惑,这表现为不连贯的 API。一旦你出错了,事情就会出错也就不足为奇了!

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).

首先,您必须弄清楚您使用的是 GET、PUT 还是 POST,在后两种情况下,您正在使用的内容类型(或类型)是什么,因为 PUT 和 POST 通常都在处理有文件传入。此外,如果您正在做的任何事情不是幂等的(即,如果您连续执行两次就像一次一样,它将是“相同的”),那么您绝对应该使用 POST;典型的例子是为一些商品付费,你绝对不想做两次,而设置你的偏好可能是幂等的。最后的复杂之处在于,将查询参数与主体混合通常是不好的风格;参数要么在查询部分中,要么在正文中(或者它们在路径中,但在这种情况下,您在概念上处理不同的资源)。

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

如果您只是处理 HTML 表单,那么您需要的两种风格的方法将是这样的:

@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=barand 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.

第一个处理像 URL 这样的 GET,/create?meg=foo&name=bar第二个处理像/create. 然而,鉴于名称“create”,我很想只使用 POST 版本而不尝试支持对查询部分中的参数进行编码;创造是那些往往不是幂等的事物之一。

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.

请注意,我假设您的创作正在制作资源(这是很好的 RESTful 编程!)所以我调整以返回正确的响应类型;它比平常涉及更多,但完全正确。

回答by suat

You should QueryParaminstead of FormParamto obtain the functionality you want

你应该QueryParam而不是FormParam获得你想要的功能