The json string is not standardized and the java entity object is interchanged

The json string is not standardized and the java entity object is interchanged

Some interface documents provided by third parties are like this (all uppercase, underscore, no camel case)

PERSON_NAME is required  

mobile is required

Sex is required 

HOBBY required

I don't know if it is an interface document written in retaliation, but I am speechless after seeing it. No naming conventions are followed at all.

His document writes like this, but we can't write like this when we build objects.

So you can use the @JsonProperty annotation

/**PERSON_NAME 必填

mobile 必填

Sex 必填

HOBBY 必填 **/

@JsonProperty("PERSON_NAME")
private String personName;
// 规范的可以不用加
private String mobile;

@JsonProperty("Sex")
private String sex;

@JsonProperty("HOBBY")
private String hobby;

pass parameters

When passed to a third party, it can be processed

   User user = new User("张三","13222222222","0","xxx") 

   ObjectMapper mapper = new ObjectMapper();
   String json = mapper.writeValueAsString(user)
   System.out.println(json); //这里打印出来就是文档所要的格式了
   return json ;

Receive parameters 

Third-party parameter passing:

{
 "PERSON_NAME":"张三",
 "mobile":"13222222222",
 "Sex":"1",
 "HOBBY":"xxx",
}

Receive transition entity:

User user = new ObjectMapper().readValue(jsonResult,User.class);

Guess you like

Origin blog.csdn.net/ss_Tina/article/details/132168669