SpringBoot返回Json对象报错(返回对象为空{})
作者:从前慢慢慢死了 时间:2022-06-30 03:31:19
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,冲冲冲!
来源:https://blog.csdn.net/qq_41010280/article/details/122589691
标签:SpringBoot,返回,Json
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
AndroidStduio3.0 使用gradle将module打包jar文件的方法
2023-07-01 06:57:20
![](https://img.aspxhome.com/file/2023/4/84224_0s.png)
java 键盘输入一个数,输出数组中指定元素的示例
2023-11-24 20:31:14
Spring @Bean注解的使用场景与案例实现
2023-11-20 04:44:22
Spring MVC的优点与核心接口_动力节点Java学院整理
2023-11-28 05:43:36
![](https://img.aspxhome.com/file/2023/9/60529_0s.jpg)
关于MVC与SpringMVC的介绍、区别、执行流程
2023-11-28 02:25:56
关于Springboot数据库配置文件明文密码加密解密的问题
2023-11-25 03:29:46
Java实现考试系统
2023-11-18 04:15:03
![](https://img.aspxhome.com/file/2023/2/59602_0s.jpg)
SpringBoot整合WebService服务的实现代码
2021-11-11 14:59:11
![](https://img.aspxhome.com/file/2023/2/60542_0s.png)
使用Java桥接模式打破继承束缚优雅实现多维度变化
2023-08-23 09:00:34
解决springboot启动失败的问题('hibernate.dialect' not set)
2023-11-09 03:21:21
Java Swing实现扫雷源码
2023-11-10 08:16:20
![](https://img.aspxhome.com/file/2023/9/59069_0s.jpg)
Java Socket实现单线程通信的方法示例
2022-04-22 15:43:02
浅谈Java中方法参数传递的问题
2021-11-06 04:24:46
![](https://img.aspxhome.com/file/2023/4/60034_0s.png)
详解如何在SpringBoot项目中使用统一返回结果
2023-10-30 16:41:21
![](https://img.aspxhome.com/file/2023/4/58714_0s.png)
java中Vector类的常用方法详解
2023-11-26 10:29:07
![](https://img.aspxhome.com/file/2023/4/60234_0s.png)
Java全面分析面向对象之继承
2023-11-23 11:55:59
![](https://img.aspxhome.com/file/2023/5/59735_0s.png)
mybatis plus新增(insert)数据获取主键id的问题
2023-08-09 10:50:52
SpringMVC自定义拦截 器登录检测功能的实现代码
2023-07-27 18:33:05
![](https://img.aspxhome.com/file/2023/5/57925_0s.png)
用Flutter开发自定义Plugin的方法示例
2023-07-05 00:19:40
![](https://img.aspxhome.com/file/2023/3/99793_0s.png)
Java虚拟机JVM性能优化(三):垃圾收集详解
2021-10-16 10:38:49
![](https://img.aspxhome.com/file/2023/5/61615_0s.png)