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

查看:27
本文介绍了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 编码.这适用于任何类型的请求(GETPOSTPUTDELETE).

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 是作为路径参数获取的,并且联系人是从请求正文中自动序列化的.

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

我上面描述的是一般规则.现在至于您的案例的具体情况,我在您的代码中注意到的一件事是您实际上没有定义任何路径参数.请记住,在您的 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) {
}

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

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 的一部分注入到变量中.请注意,网址参数被视为网址的一部分.

示例:给定 URL http://services.example.com/contacts/20578,我可以定义:

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 请求(GETPOSTPUTDELETE).

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天全站免登陆