sping boot returns the JSON format definition

Source: Internet
Author: User
Tags format definition

Reprint Please specify http://www.cnblogs.com/majianming/p/8491020.html

During the writing of the project, an issue was encountered that needed to define the format of the returned JSON field

For example, in an entity attribute, I have a field that is username, so the need to appear in JSON is user_name, that is, using the underscore name method

You can then use @JsonProperty this annotation to customize the @JsonProperty ("user_name")

But if all my attributes are to be used in this way, do I add one by one?

In fact, just define Jackson's property naming policy to add

(yml format)

Spring

Jackson

Property-naming-strategy:snake_case

(propreties format)

Spring.jackson.property-naming-strategy=snake_case

You can do it.

All policies are

Strategy Example Note
Lower_camel_case UserName Default Policy
Kebab_case User-name
Lower_case Username
Snake_case User_name
Upper_camel_case UserName

There seems to be no problem here, but if I have only one entity, the attribute name needs to be mapped to one of the above conditions,

But there are many attribute names, too lazy to add it?

In fact, there are methods, as long as the need to add the entity@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)这样的注解就可以了

The strategies you can use here are also consistent with those in the table above

Above

Reference

    1. Http://www.baeldung.com/jackson-advanced-annotations

    2. https://stackoverflow.com/a/40730352

Reprint Please specify http://www.cnblogs.com/majianming/p/8491020.html

sping boot returns the JSON format definition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.