SpringBoot返回Json对象报错(返回对象为空{})
1 需求描述
我们现在要干一个什么事情呢,我们要在浏览器输入一个请求地址,然后我们的后端就给我返回一个User对象即可,并且我希望以Json的格式返回。这个需求很明确,我们先直观的展示一下效果。
发送请求:
接受结果:
2 代码展示
行了,明确了需求我们开始整活儿。首先我们老规矩还是先展示一下目录结构(其中标红的文件使我们今天要用到的):
接下来是具体的文件内容首先呢我们展示一下User.java文件
package com.example.springboot02.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import java.io.Serializable; @Entity //表示为实体类 public class User implements Serializable { @Id //Jpa 注解可以不写 private Long id; //Jpa 注解可以不写,下边一样 @Column(nullable = false, unique = true) private String userName; @Column(nullable = false) private String passWord; @Column(nullable = false, unique = true) private String email; @Column(nullable = true, unique = true) private String nickName; @Column(nullable = false) private String regTime; // 有参构造函数 public User(Long id, String userName, String passWord, String email, String nickName, String regTime) { this.id = id; this.userName = userName; this.passWord = passWord; this.email = email; this.nickName = nickName; this.regTime = regTime; } // 无参构造函数 public User() { } }
接下来Usercontroller.java文件的内容:
package com.example.springboot02.controller; import com.example.springboot02.entity.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @RequestMapping(value = "/getUser") public User getUser() { return new User(0L,"zxd", "12345", "zxd@thu.edu.cn", "zxd","123"); } }
好了齐活了,我们来测试一下:
没想到吧结果却是这个鬼样子!没返回!!!为啥呢?
3 原因分析
其实在Springboot中,我们使用 @RestController 注解可以让我们直接返回Json对象,可以将对象转换成Json格式,然而这一切都依赖于User类的Getter/Setter函数而我们的代码中却没有写,最终导致了我么得到了空的对象。
4 解决方案
那就加Getter/Setter函数就好喽~
我们更新一下User.java文件:
package com.example.springboot02.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import java.io.Serializable; @Entity public class User implements Serializable { @Id private Long id; @Column(nullable = false, unique = true) private String userName; @Column(nullable = false) private String passWord; @Column(nullable = false, unique = true) private String email; @Column(nullable = true, unique = true) private String nickName; @Column(nullable = false) private String regTime; public User(Long id, String userName, String passWord, String email, String nickName, String regTime) { this.id = id; this.userName = userName; this.passWord = passWord; this.email = email; this.nickName = nickName; this.regTime = regTime; } public User() { } public void setId(Long id) { this.id = id; } public void setUserName(String userName) { this.userName = userName; } public void setPassWord(String passWord) { this.passWord = passWord; } public void setEmail(String email) { this.email = email; } public void setNickName(String nickName) { this.nickName = nickName; } public void setRegTime(String regTime) { this.regTime = regTime; } public Long getId() { return id; } public String getUserName() { return userName; } public String getPassWord() { return passWord; } public String getEmail() { return email; } public String getNickName() { return nickName; } public String getRegTime() { return regTime; } } //
5 效果展示
这次就行了哦
6 结束语
本来今天想讲一下springboot 整合Redis的,无意中触发了这个bug,就来记录了一下希望大家引以为戒,明天继续sprinboot实战整合redis,冲冲冲!
上一篇:springcloud feign传输List的坑及解决
栏 目:JAVA代码
下一篇:mybatisplus 复合主键(多主键) CRUD示例详解
本文标题:SpringBoot返回Json对象报错(返回对象为空{})
本文地址:http://www.codeinn.net/misctech/224309.html