• notice
  • Congratulations on the launch of the Sought Tech site

Invalid problem of using JSONField to transfer parameter name when requestBody annotation receives parameters in spring

question:

  In the springboot project, use the @RequestBody annotation to receive the json parameter in the body of the post request. which is:

@RequestMapping(value = "/get-user", method = RequestMethod.POST)
    public String getUser(@RequestBody User user) {
        System.out.println("user_name:" + user.getUserName());
        return JSON.toJSONString(user);
    }


    @Getter
    @Setter
    public  class User {

        private String id;

        @JSONField(name = "user_name" )
        private String userName;
   }

  At this time, when the passed json parameter, if the parameter name is not userName but user_name, it will not be received, which is equivalent to using the @JSONField annotation has no effect.

 

solution:

  Use the @JsonProperty(value = "user_name") annotation instead, as follows:

@Getter
    @Setter
    public  class User {

    private String id;

    @JsonProperty(value = "user_name" )
    private String userName;
    }

  why? The reason is that the @RequestBody annotation is parsed by Jackson by default. The @JsonProperty annotation is Jackson's, and the @JSONField annotation is FastJson, so it will lead to the invalidation of the @JSONField annotation.

 


Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

buy lipitor 10mg pills & lt;a href="https://lipiws.top/"& gt;buy atorvastatin 40mg generic& lt;/a& gt; buy generic lipitor online

Xcwjnu

2024-03-07

Leave a Reply

+