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

发布于 2024-12-10 08:23:04 字数 723 浏览 0 评论 0原文

我想开发rest api.例如: http://localhost:8080/TestSomeWay/resources/test/create ?meg=sadasd&name=sadasd 并从 urlparams exp."meg"&"name" 获取参数 我正在使用 jersey 开发 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();

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁与争疯 2024-12-17 08:23:04

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

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

如果您只处理 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();
}

第一种处理 URL 上的 GET,如 /create?meg=foo&name=bar 第二个处理 POST 到像 /create 这样的 URL。然而,考虑到名称“create”,我会倾向于只使用 POST 版本,而不是尝试支持在查询部分对参数进行编码;创造是往往不是幂等的事物之一。

请注意,我假设您的创建正在制作资源(这是很好的 RESTful 编程!),因此我进行了调整以返回正确类型的响应;它比平常更复杂一些,但确实是正确的事情。

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.

懵少女 2024-12-17 08:23:04

您应该使用 QueryParam 而不是 FormParam 来获取您想要的功能

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文