Spring Boot learning 2.1 - JSON data returned by the backend

The current Web-based development model front and rear ends are separated manner, JSON is the main way of front and rear ends of each data transmission.

JSON dependency we have in the default configuration in springboot completed

Demo: We create a class in Bean Book folder:

package com.example.demo.bean;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@ConfigurationProperties(prefix = "book")
public class Book {
    private String name;
    private String author;
    @JsonIgnore
    private double price;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date publicationDate;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public Date getPublicationDate() {
        return publicationDate;
    }
    public void setPublicationDate(Date publicationDate) {
        this.publicationDate = publicationDate;
    }
}

Then add the following code to program the controller class:

    @GetMapping("/book")
    @ResponseBody
    public Book book(){
        Book book = new Book();
        book.setAuthor("Rod Johnson ");
        book.setName("Spring Boot");
        book.setPrice(36.50);
        book.setPublicationDate(new Date());
        return book;
    }

In the browser, enter: localhost: 8000 / book you can see the JSON data returned on a page

Published 58 original articles · won praise 31 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_37504771/article/details/95678652