Jersey POST方法接收空值作为参数 [英] Jersey POST Method is receiving null values as parameters

查看:179
本文介绍了Jersey POST方法接收空值作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey开发RESTful服务,它可以很好地使用GET方法。但是我无法使用POST方法和JSON或文本参数。这就是我做的:

I am developing RESTful services with Jersey and it works great with GET methods. However I can't make it work with POST methods and JSON or text parameters. These is what I did:

@Path("/method/")
@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
@Produces({MediaType.APPLICATION_JSON})
public ResponseObject method(@Context Request request, @PathParam("ob1") Object obj1, @PathParam("obj2") String obj2) {
...
}

我只获得所有参数的空值。我试图只使用一个字符串作为参数,它也不起作用...我试图从IOS访问这些方法,也许这是问题之一。但是我一直在嗅探我的局域网,我可以在数据包正文中看到正确的参数......这是正确的吗?

I am only getting null values for all params. I have tried to use just a string as parameter and it doesn't work either ... I am trying to access these methods from IOS and maybe that's one of the problems. However I have been sniffing my LAN and I can see the correct parameters in the packet body ... is this correct??

我从XCode发送了不同的正文内容:

I have sent from XCode different body contents as:

obj1={"id1": "value1", "id2" : "value2"}&obj2=xxxx

和:

{"id1": "value1", "id2" : "value2"},xxxx

虽然我一直在玩@QueryParam和@PathParam没有结果......总是空...

while I have been playing with @QueryParam and @PathParam without results...always null...

感谢您的帮助!

推荐答案

路径参数是一部分请求匹配特定模式的URL。因此,对于可以指定为路径参数的内容存在字符限制 - 特别是任何特殊字符都需要进行URL编码。这适用于任何类型的请求( GET POST PUT DELETE )。

A path parameter is a portion of the request URL matching a particular pattern. As such, there are character limitations on what can be specified as a path param - in particular any special characters need to be URL encoded. This applies the same across any kind of request (GET, POST, PUT, DELETE).

作为一般规则,您应该将路径参数限制为简单值(如标识符或资源端点) - 更复杂的数据应通过请求参数传递给REST服务或请求正文本身。这是一种混合方法,它将实体标识符作为路径参数传递,并将请求正文中的实体数据传递:

As a general rule, you should restrict your path parameters to simple values like identifiers, or resource endpoints - more complex data should be passed to the REST service via request parameters or the request body itself. Here's a mixed approach that passes an entity identifier as a path parameter, and the entity data in the request body:

@Path("/contacts/{id}")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateContact(@PathParam final String contactId, Contact contact) {
}

在上面的示例中, contactId <获取/ em>作为路径参数,并从请求正文自动序列化联系人。

In the above example, the contactId is obtained as a path parameter and the contact is serialized automatically from the request body.

我上面描述的是 general 规则。现在关于你的情况的细节,我在你的代码中注意到的一件事是你实际上没有定义任何路径参数。请记住,在使用REST方法之前,必须将它们定义为 @Path 注释的一部分:

What I described above are general rules. Now as to the specifics of your case, one thing I notice in your code is that you don't actually define any path parameters. Remember that they have to be defined as part of your @Path annotation, before being consumed in your REST method:

@Path("/method/{obj1}/{obj2}")
public ResponseObject method(@Context Request request, @PathParam("obj1") Object obj1, @PathParam("obj2") String obj2) {
}

通过上述更改您的参数假设您已在客户端对URL进行了正确编码,则不应再显示为null。

With the above changes your parameters should no longer show up as being null, assuming you have properly encoded the URL on the client side.

*编辑*

根据您的评论,我发现您需要更熟悉JAX-RS规范和各种参数类型。我建议您阅读 RESTEasy JAX-RS文档。它有一些供应商特定的实现细节,但总的来说是JAX-RS的优秀指南。

Based on your comment, I see you need to become more familiar with the JAX-RS specification and the various parameter types. I recommend reading through the RESTEasy JAX-RS Documentation. It has some vendor specific implementation details but all in all is an excellent guide to JAX-RS.

目的 :用于将请求URL的一部分注入变量。请注意,网址参数被视为网址的一部分。

Purpose: Used to inject a portion of the request URL into a variable. Note that URL parameters are not considered part of the URL.

示例 :鉴于网址 http://services.example.com/contacts/20578 ,我可以define:

Example: Given the URL http://services.example.com/contacts/20578, I can define:

@Path("/contacts/{id}")

我可以从中注入 @PathParam(id)

public Response getContact(@PathParam("id") final String identifier);

这适用于任何类型的HTTP请求( GET POST PUT DELETE )。

This works for any kind of HTTP request (GET, POST, PUT, DELETE).

目的 :用于注入一部分查询字符串或将编码数据形成变量。查询字符串是之后的URL部分。当请求类型为 application / x-www-form-urlencoded 时,表单编码数据是在HTTP请求正文中传递的URL编码的名称/值对数据。通常,查询参数作为GET请求的URL字符串的一部分传递,并在POST请求的请求体中传递。

Purpose: Used to inject a portion of the query string or form encoded data into a variable. The query string is that portion of your URL after the ?. Form encoded data are the URL encoded name/value pair data passed in the body of an HTTP request, when the request type is application/x-www-form-urlencoded. Typically, query parameters are passed as part of the URL string for GET requests, and in the request body for POST requests.

示例:给定URL http://services.example.com/contacts?group=Business ,我可以注入 @QueryParam( group)

Example: Given the URL http://services.example.com/contacts?group=Business, I can inject a @QueryParam("group")

public Response getContactsInGroup(@QueryParam("group") final String groupName);

将查询参数与POST请求一起使用是非常典型的,但如果请求类型为 application / x-www-form-urlencoded

It's atypical to use query parameters with a POST request, but it is possible if the request type is application/x-www-form-urlencoded:

@POST
@Path("/contacts")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createContact(@QueryParam("contact") final Contact contactData, @QueryParam("metadata") final String metaData);

这些只是高级示例,请通读文档我链接以获得每个参数类型如何工作的更好示例,以及何时使用哪个一个。

These are just high level examples, please read through the documentation I linked to get a better example of how each parameter type works, and when to use which one.

这篇关于Jersey POST方法接收空值作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆