The property name of the json object returned by the custom JavaBean in springboot is capitalized and lowercased

The property name of the json object returned by the custom JavaBean in springboot is capitalized and lowercased

During the development process, it was found that the data returned by the query had a problem with the uppercase and lowercase format of the custom JavaBean attribute value, which caused the front end to fail to receive the data. There are currently four solutions. According to the experience of the boss, the first two are the simplest. Convenient, the latter two are more general methods.

The specific reasons are as follows:

insert image description here

1. Inheritance class

Generally, at vothe layer (you can understand it by yourself, Alibaba’s programming specification), you can customize JavaBean to encapsulate data and return it to the front end. You need to use multi-table queries, or you need to create additional attributes.

For example: the best way for human javabean is to inherit a student javabean and add new attributes, so that there is no problem in human javabean, and the data returned by student javabean is also normal, and there will be no bugs caused by @Data annotation .

2. Manually add the Get method

This may be a bug caused by the @Data annotation in Lombok. Manually adding get and set methods can also solve this problem. (Try the returned format is normal)

3. @JsonProperty

Other bloggers' solutions: add annotations to the field attributes to specify the @JsonProperty("aAnimalId")serialized name, get方法and add annotations to the field attributes @JsonIgnore.

insert image description here

The result of my attempt: add annotations to the field properties @JsonProperty("aAnimalId"), and the key of the data returned by the interface will return to normal, which may be the reason why I mixed it with @Data. But after analyzing it, the results are as follows:

insert image description here

Result analysis:

Change the first letter to size, so as to offset each other with springboot returning the json object to change the letter to lowercase, so that the normal format can be returned (self-understanding, not sure if it is right), but the problem is solved. You can try it, it is a good way to solve the development problem.

Four, spring-boot json (jackson) attribute naming strategy

The global configuration naming strategy has not been tried, and the boss asked me to understand it, and there will be certain gains.

spring:
  jackson:
    property-naming-strategy: SNAKE_CASE

记录每一个学习瞬间

Guess you like

Origin blog.csdn.net/qq_51601665/article/details/131385895