@JsonProperty不适用于Content-Type:application / x-www-form-urlencoded [英] @JsonProperty not working for Content-Type : application/x-www-form-urlencoded

查看:166
本文介绍了@JsonProperty不适用于Content-Type:application / x-www-form-urlencoded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当REST API映射到Java对象时,REST API采用输入内容类型:application / x-www-form-urlencoded,例如

The REST API takes input content type : application/x-www-form-urlencoded, when it is mapped to a Java Object, like

 public class MyRequest {

    @JsonProperty("my_name")
    private String myName;

    @JsonProperty("my_phone")
    private String myPhone;

    //Getters and Setters of myName and myPhone.

    }

在表单输入请求中,我设置my_name的值和my_phone,但MyRequest对象附带myName和myPhone为null。

In form input request, I am setting values of my_name and my_phone but the MyRequest object comes with myName and myPhone as null.

我正在使用Jackson-annotations 2.3 jar

I am using Jackson-annotations 2.3 jar

任何建议可能出错?

推荐答案

我最近使用SpringMVC和Jackson遇到了同样的问题!

I had the same problem recently using SpringMVC and Jackson!

在Spring中,当您明确将端点配置为仅使用 application / x-www-form-urlencoded 请求时,Spring可以序列化为你的POJO课程,但它不使用杰克逊,因为它不是JSON。

In Spring, when you explicit configure your endpoint to consume only application/x-www-form-urlencoded requests Spring is able to serialize into your POJO classes but it does not use Jackson because it isn't JSON.

因此,为了让那些杰克逊注释使用您的POJO工作,您必须:

So, in order to get those Jackson annotations working using your POJO, you'll have to:


  1. 将您的数据作为地图

  2. 使用Jackson的ObjectMapper解析您的数据地图

在我的情况下,使用Spring我可以使用以下代码解决此问题:

In my case, with Spring I could resolve this problem with the following code:

@RequestMapping(
        value = "/rest/sth",
        method = RequestMethod.POST
)
public ResponseEntity<String> create(@RequestBody MultiValueMap paramMap) { ... }

当你从中删除consumes属性时 @RequestMapping 注释你必须使用 @RequestBody 否则Spring将无法将你的地图识别为有效参数。

When you remove the "consumes" attribute from @RequestMapping annotation you have to use @RequestBody or else Spring won't be able to identify your map as a valid parameter.

您可能会注意到的一件事是 MultiValueMap 不是常规地图。每个元素值都是 LinkedList ,因为http表单数据可以重复值,因此这些值将添加到该链表中。

One thing that you'll probably notice is that MultiValueMap is not a regular map. Each element value is a LinkedList because http form data can repeat values and therefore those values would be added to that linked list.

考虑到这一点,这里有一个简单的代码来获取第一个元素并创建另一个地图转换为您的POJO:

With that in mind, here is a simple code to get the first element and create another map to convert to your POJO:

    HashMap<String, Object> newMap = new HashMap<>();
    Arrays.asList(new String[]{"my_name", "my_phone"})
            .forEach( k -> newMap.put(k, ((List<?>) paramMap.get(k)).get(0)));

    MyRequest myrequest = new ObjectMapper().convertValue(newMap, MyRequest.class);

我希望它可以帮助你如何帮助我:)

I hope it can help you how it helped me :)

这篇关于@JsonProperty不适用于Content-Type:application / x-www-form-urlencoded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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