While returning JSON data is a common form of interaction in WEB projects, it’s easy to do in Spring Boot. So easy!!!

The foundation you need to have

  • What is Spring Boot?
  • Spring Boot core configuration file details
  • Spring Boot can be enabled in two ways
  • Spring Boot automatic configuration principle, actual combat
  • Spring Boot 2.x startup process source code analysis

For more information, please reply in the background of wechat public account of Java Technology stack keyword: boot.

How do I return JSON data?

Returning JSON data in Spring Boot is as simple as following steps.

Join the rely on

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> < version > 2.0.4. RELEASE < / version > < / parent > < the dependency > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-starter-web</artifactId> </dependency>Copy the code

In addition to the parent dependency that Spring Boot must come with, simply add the spring-boot-starter-Web package, which automatically contains all jSON-processed packages, as shown below.

Thank you for sharing this plugin with planet Knowledge, simple and good, click on the bottom of the article to read the original text, you can join to learn.

Returns a JSON data format definition

1) Define the return method

Define it as @RestController on the Controller class or @responseBody on the method to output data in the Body field.

Here is an example:

@RestController
public class JsonTest {

	@GetMapping(value = "/user/{userId}")
	public User getUserInfo(@PathVariable("userId") String userId) {
		User user = new User("Java Technology Stack", 18);
		user.setId(Long.valueOf(userId));
		returnuser; }}Copy the code
2) Customize the output format

The above method returns the object directly, which is automatically converted to JSON format, but is the default tag. You can customize the JSON format using the following tags.

public class User {

	@JsonProperty("user-name")
	private String userName;

	private Long id;

	private Integer age;

	@JsonIgnore
	private String address;

	@JsonInclude(JsonInclude.Include.NON_NULL)
	private String memo;
	
	// get setSlightly}Copy the code

Program output:

{"id": 1,"age": 18."user-name":"Java Technology Stack"}
Copy the code

Several common annotations are demonstrated above.

@jsonProperty: The name of the attribute tag that can be customized;

@jsonignore: Can be used to ignore tags that do not want to output an attribute;

@jsoninclude: tags that can be used to dynamically include attributes, such as attributes that do not contain null values;

See this package for more notes:

How do I interchange objects and Json manually?

Jackson – databind package. There is a com fasterxml. Jackson. Databind. ObjectMapper can complete object and Json data transfers, the following is a simple example of cooperation.

ObjectMapper objectMapper = new ObjectMapper();

String userJsonStr = objectMapper.writeValueAsString(user);

User jsonUser = objectMapper.readValue(userJsonStr, User.class);
Copy the code

See this package for more information on how it works.

“Spring Boot returns XML data” stack length is desperately writing, two days to share.

While returning JSON data is a common form of interaction in WEB projects, it’s easy to do in Spring Boot. So easy!!!

Well, today’s sharing is over here, more Spring Boot articles are being written, follow the Java technology stack wechat official account to get the first time push.

In the public account background reply: boot, can also get stack length arrangement of the previous Spring Boot tutorial, are actual combat dry goods, the following is only part of the preview.

  • Several ways for Spring Boot to read configurations
  • How to verify Spring Boot parameters?
  • Spring Boot most core 25 notes!
  • Spring Boot 2.x startup process source code analysis
  • Spring Boot 2.x new features summary and migration guide