SpringBoot返回Json對象報(bào)錯(cuò)(返回對象為空{(diào)})
1 需求描述
我們現(xiàn)在要干一個(gè)什么事情呢,我們要在瀏覽器輸入一個(gè)請求地址,然后我們的后端就給我返回一個(gè)User對象即可,并且我希望以Json的格式返回。這個(gè)需求很明確,我們先直觀的展示一下效果。
發(fā)送請求:

接受結(jié)果:

2 代碼展示
行了,明確了需求我們開始整活兒。首先我們老規(guī)矩還是先展示一下目錄結(jié)構(gòu)(其中標(biāo)紅的文件使我們今天要用到的):

接下來是具體的文件內(nèi)容首先呢我們展示一下User.java文件
package com.example.springboot02.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
@Entity //表示為實(shí)體類
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;
// 有參構(gòu)造函數(shù)
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;
}
// 無參構(gòu)造函數(shù)
public User() {
}
}
接下來Usercontroller.java文件的內(nèi)容:
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");
}
}
好了齊活了,我們來測試一下:


沒想到吧結(jié)果卻是這個(gè)鬼樣子!沒返回?。。樯赌??
3 原因分析
其實(shí)在Springboot中,我們使用 @RestController 注解可以讓我們直接返回Json對象,可以將對象轉(zhuǎn)換成Json格式,然而這一切都依賴于User類的Getter/Setter函數(shù)而我們的代碼中卻沒有寫,最終導(dǎo)致了我么得到了空的對象。
4 解決方案
那就加Getter/Setter函數(shù)就好嘍~
我們更新一下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 結(jié)束語
本來今天想講一下springboot 整合Redis的,無意中觸發(fā)了這個(gè)bug,就來記錄了一下希望大家引以為戒,明天繼續(xù)sprinboot實(shí)戰(zhàn)整合redis,沖沖沖!
到此這篇關(guān)于SpringBoot返回Json對象報(bào)錯(cuò)(返回對象為空{(diào)})的文章就介紹到這了,更多相關(guān)SpringBoot返回Json報(bào)錯(cuò)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Spring?@Lazy注解為什么能破解死循環(huán)
SpringMVC框架post提交數(shù)據(jù)庫出現(xiàn)亂碼解決方案
分布式醫(yī)療掛號系統(tǒng)EasyExcel導(dǎo)入導(dǎo)出數(shù)據(jù)字典的使用
Javaweb基礎(chǔ)入門HTML之table與form
Spring AOP 對象內(nèi)部方法間的嵌套調(diào)用方式
舉例講解Java的Hibernate框架中的多對一和一對多映射
解決Springboot集成Redis集群配置公網(wǎng)IP連接報(bào)私網(wǎng)IP連接失敗問題

