赞
踩
JSON是目前主流的前后端数据传输方式,Spring MVC中使用消息转换器HttpMessageConverter对JSON的转换提供了很好的支持,在Spring Boot中更进一步,对相关配置做了更进一步的简化。默认情况下,当开发者新创建一个Spring Boot项目后,添加Web依赖,代码如下:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
这个依赖中默认加入了jackson-databind作为JSON处理器,此时不需要添加额外的JSON处理器就能返回一段JSON了。
新建一个springboot项目,具体截图
创建一个Book实体类:
Book.java
- package com.shrimpking.code01;
-
- import com.fasterxml.jackson.annotation.JsonFormat;
- import com.fasterxml.jackson.annotation.JsonIgnore;
-
- import java.util.Date;
-
- /**
- * Created by IntelliJ IDEA.
- *
- * @Author : Shrimpking
- * @create 2023/6/4 11:02
- */
- public class Book
- {
- private String name;
- private String author;
- /**
- * 忽略此项
- */
- @JsonIgnore
- private Float price;
- @JsonFormat(pattern = "yyyy-MM-dd")
- private Date publicationDate;
-
- public Book()
- {
- }
-
- public Book(String name, String author, Float price, Date publicationDate)
- {
- this.name = name;
- this.author = author;
- this.price = price;
- this.publicationDate = 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;
- }
-
- @Override
- public String toString()
- {
- return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", publicationDate=" + publicationDate + '}';
- }
- }
然后创建BookController,返回Book对象即可:
BookController.java
- package com.shrimpking.code01;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- import java.util.Date;
-
- /**
- * Created by IntelliJ IDEA.
- *
- * @Author : Shrimpking
- * @create 2023/6/4 11:05
- */
- @Controller
- public class BookController
- {
- @GetMapping("/book")
- @ResponseBody
- public Book test()
- {
- Book book = new Book();
- book.setAuthor("作者");
- book.setName("图书名");
- book.setPrice(999.00f);
- book.setPublicationDate(new Date());
- return book;
- }
- }
------------------------------------------------------------------------------------------------------------------------------
当然,如果需要频繁地用到@ResponseBody注解,那么可以采用@RestController组合注解代替@Controller和@ResponseBody,代码如下:
Book.java
- package com.shrimpking.code02;
-
- /**
- * Created by IntelliJ IDEA.
- *
- * @Author : Shrimpking
- * @create 2023/6/4 11:11
- */
- public class Book
- {
- private String name;
- private String author;
-
- 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;
- }
-
- @Override
- public String toString()
- {
- return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + '}';
- }
- }
OtherController.java
- package com.shrimpking.code02;
-
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * Created by IntelliJ IDEA.
- *
- * @Author : Shrimpking
- * @create 2023/6/4 11:11
- */
- @RestController
- /*
- 使用@RestController替代@Controller 和@ResponseBody
- */
- public class OtherController
- {
- @GetMapping("/otherbook")
- public Book test()
- {
- Book book = new Book();
- book.setName("什么书");
- book.setAuthor("哪个作者");
- return book;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。