简体   繁体   English

JAX-RS:使用 JSON POST 数据而不将 Content-Type 标头设置为 application/json

[英]JAX-RS: Consuming JSON POST data without setting Content-Type header to application/json

I have implemented a REST service using Jersey that takes JSON POST data and creates an object from a POJO model.我已经使用 Jersey 实现了一个 REST 服务,它采用 JSON POST 数据并从 POJO 模型创建一个对象。 However, in order for this to work, I have to set the Content-Type to application/json (ie, -H "Content-Type: application/json" ).但是,为了使其工作,我必须将 Content-Type 设置为 application/json(即-H "Content-Type: application/json" )。 What I'd like is to be able to consume JSON POST request body without the user having to set the header, basically like Elasticsearch works:我想要的是能够使用 JSON POST 请求正文而无需用户设置标头,基本上就像 Elasticsearch 一样:

POST /test_index/_search?search_type=count
{
   "aggs": {
      "nested_authors": {
         "nested": {
            "path": "authors"
         },
         "aggs": {
            "author_last_names": {
               "terms": {
                  "field": "authors.last_name"
               }
            }
         }
      }
   }
} 

Here's the relevant code:这是相关的代码:

@POST
@Path("/person")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postPerson(PostBody pb) {
    System.out.println(pb.getEmails());
}

I faced a similar problem.我遇到了类似的问题。 Since in my opinion a well defined API - which doesn't share its endpoints with any other system - should not need to depend on clients specifying the correct Content-Type , I created a workaround.由于在我看来定义明确的 API - 不与任何其他系统共享其端点 - 不应该依赖于指定正确Content-Type客户端,所以我创建了一个解决方法。 In this workaround, I add an annotation to those resource methods where I want Jersey to always attempt to read the input according to a server-defined Content-Type .在此解决方法中,我向那些希望 Jersey 始终尝试根据服务器定义的Content-Type读取输入的资源方法添加注释。 Whenever this annotation is present, a ResourceFilter will override the Content-Type header in the request, to whatever is specified in the annotation.每当此注释出现时, ResourceFilter将覆盖请求中的Content-Type标头,以注释中指定的任何内容。

I have detailed the process in my answer here .我在我的回答中详细说明了这个过程。

Figured it out.弄清楚了。 I'm now accepting "application/json" and "application/x-www-form-urlencoded" content types.我现在接受“application/json”和“application/x-www-form-urlencoded”内容类型。 Here's the code:这是代码:

@POST
@Path("/person")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_FORM_URLENCODED})
@Produces(MediaType.APPLICATION_JSON)
public Response postPerson(String body) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    PostBody pb = mapper.readValue(body, PostBody.class);
    System.out.println(pb.getEmails());
}

Though, after thinking for a bit, I probably should require the Content-Type header considering it contains a JSON request body, but that's another discussion entirely.不过,考虑一下,考虑到它包含一个 JSON 请求正文,我可能应该需要 Content-Type 标头,但这完全是另一个讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM