• SpringBoot返回JSON数据


    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;
        }
    }

  • 相关阅读:
    node 读取文件
    jQuery全局事件处理函数
    可以发送不同源请求的方式
    ajax 高度封装的函数
    jQuery中AJAX的回调
    jQuery中对AJAX的封装
    ajax 基本的封装
    AJAX 返回数据问题
    ajax 关于响应类型
    动态渲染数据到表格中
  • 原文地址:https://www.cnblogs.com/mingforyou/p/14611546.html
Copyright © 2020-2023  润新知