SpringBoot默认加入了jackson-databind作为JSON处理器。

 

package com.example.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.util.Date;

public class Book implements Serializable {
    private String name;
    private String author;
    @JsonIgnore
    private Float 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 Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public Date getPublicationDate() {
        return publicationDate;
    }

    public void setPublicationDate(Date publicationDate) {
        this.publicationDate = publicationDate;
    }
}

 

package com.example.controller;

import com.example.pojo.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class BookController {
    @GetMapping("/book")
    @ResponseBody
    public Book book(){
        Book book = new Book();
        book.setAuthor("罗贯中");
        book.setName("三国演义");
        book.setPrice(48f);
        book.setPublicationDate(new Date());
        return book;
    }
}

 

SpringBoot返回JSON数据

 

相关文章:

  • 2022-12-23
  • 2023-03-22
  • 2022-02-17
  • 2022-12-23
  • 2022-01-09
  • 2021-07-02
  • 2022-01-17
猜你喜欢
  • 2021-07-22
  • 2021-09-29
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2021-11-01
  • 2021-05-27
相关资源
相似解决方案