当前位置:   article > 正文

springboot 返回JSON数据_springboot返回json对象

springboot返回json对象

JSON是目前主流的前后端数据传输方式,Spring MVC中使用消息转换器HttpMessageConverter对JSON的转换提供了很好的支持,在Spring Boot中更进一步,对相关配置做了更进一步的简化。默认情况下,当开发者新创建一个Spring Boot项目后,添加Web依赖,代码如下:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

这个依赖中默认加入了jackson-databind作为JSON处理器,此时不需要添加额外的JSON处理器就能返回一段JSON了。

新建一个springboot项目,具体截图

创建一个Book实体类:

Book.java

  1. package com.shrimpking.code01;
  2. import com.fasterxml.jackson.annotation.JsonFormat;
  3. import com.fasterxml.jackson.annotation.JsonIgnore;
  4. import java.util.Date;
  5. /**
  6. * Created by IntelliJ IDEA.
  7. *
  8. * @Author : Shrimpking
  9. * @create 2023/6/4 11:02
  10. */
  11. public class Book
  12. {
  13. private String name;
  14. private String author;
  15. /**
  16. * 忽略此项
  17. */
  18. @JsonIgnore
  19. private Float price;
  20. @JsonFormat(pattern = "yyyy-MM-dd")
  21. private Date publicationDate;
  22. public Book()
  23. {
  24. }
  25. public Book(String name, String author, Float price, Date publicationDate)
  26. {
  27. this.name = name;
  28. this.author = author;
  29. this.price = price;
  30. this.publicationDate = publicationDate;
  31. }
  32. public String getName()
  33. {
  34. return name;
  35. }
  36. public void setName(String name)
  37. {
  38. this.name = name;
  39. }
  40. public String getAuthor()
  41. {
  42. return author;
  43. }
  44. public void setAuthor(String author)
  45. {
  46. this.author = author;
  47. }
  48. public Float getPrice()
  49. {
  50. return price;
  51. }
  52. public void setPrice(Float price)
  53. {
  54. this.price = price;
  55. }
  56. public Date getPublicationDate()
  57. {
  58. return publicationDate;
  59. }
  60. public void setPublicationDate(Date publicationDate)
  61. {
  62. this.publicationDate = publicationDate;
  63. }
  64. @Override
  65. public String toString()
  66. {
  67. return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", publicationDate=" + publicationDate + '}';
  68. }
  69. }

然后创建BookController,返回Book对象即可:

BookController.java

  1. package com.shrimpking.code01;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. import java.util.Date;
  6. /**
  7. * Created by IntelliJ IDEA.
  8. *
  9. * @Author : Shrimpking
  10. * @create 2023/6/4 11:05
  11. */
  12. @Controller
  13. public class BookController
  14. {
  15. @GetMapping("/book")
  16. @ResponseBody
  17. public Book test()
  18. {
  19. Book book = new Book();
  20. book.setAuthor("作者");
  21. book.setName("图书名");
  22. book.setPrice(999.00f);
  23. book.setPublicationDate(new Date());
  24. return book;
  25. }
  26. }

------------------------------------------------------------------------------------------------------------------------------

 当然,如果需要频繁地用到@ResponseBody注解,那么可以采用@RestController组合注解代替@Controller和@ResponseBody,代码如下:

Book.java

  1. package com.shrimpking.code02;
  2. /**
  3. * Created by IntelliJ IDEA.
  4. *
  5. * @Author : Shrimpking
  6. * @create 2023/6/4 11:11
  7. */
  8. public class Book
  9. {
  10. private String name;
  11. private String author;
  12. public String getName()
  13. {
  14. return name;
  15. }
  16. public void setName(String name)
  17. {
  18. this.name = name;
  19. }
  20. public String getAuthor()
  21. {
  22. return author;
  23. }
  24. public void setAuthor(String author)
  25. {
  26. this.author = author;
  27. }
  28. @Override
  29. public String toString()
  30. {
  31. return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + '}';
  32. }
  33. }

OtherController.java

  1. package com.shrimpking.code02;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * Created by IntelliJ IDEA.
  6. *
  7. * @Author : Shrimpking
  8. * @create 2023/6/4 11:11
  9. */
  10. @RestController
  11. /*
  12. 使用@RestController替代@Controller 和@ResponseBody
  13. */
  14. public class OtherController
  15. {
  16. @GetMapping("/otherbook")
  17. public Book test()
  18. {
  19. Book book = new Book();
  20. book.setName("什么书");
  21. book.setAuthor("哪个作者");
  22. return book;
  23. }
  24. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/619906
推荐阅读
相关标签
  

闽ICP备14008679号