In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you SpringBoot return Json object error report how to do, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
1 description of requirements
What are we going to do now? we enter a request address in the browser, and then our back end just returns a User object to me, and I want to return it in Json format. This requirement is very clear, let's first visually show the effect.
Send request:
Accept the result:
2 code display
All right, let's get to work when the needs are clear. First of all, our old rule is to show the directory structure (the red files are what we are going to use today):
Next is the specific content of the file. First of all, let's show the User.java file.
Package com.example.springboot02.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import java.io.Serializable;@Entity / / is expressed as entity class public class User implements Serializable {@ Id / / Jpa annotations without writing private Long id; / / Jpa annotations, as follows: @ 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; / / Parametric constructor 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;} / / No-parameter constructor public User () {}}
Next, the contents of the Usercontroller.java file:
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;@RestControllerpublic class UserController {@ RequestMapping (value = "/ getUser") public User getUser () {return new User (0L, "zxd", "12345", "zxd@thu.edu.cn", "zxd", "123");}}
All right, it's done. Let's test it:
I didn't expect it to turn out to be like this! Did not return! Why is that?
(3) cause analysis
In fact, in Springboot, we use the @ RestController annotation to allow us to directly return the Json object and convert the object to Json format, but all this depends on the Getter/Setter function of the User class, which is not written in our code, resulting in us getting an empty object.
4 solution
Then just add the Getter/Setter function.
Let's update the User.java file:
Package com.example.springboot02.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import java.io.Serializable;@Entitypublic 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 effect display
This time will do.
The above is all the contents of the article "how to report an error in the Json object returned by SpringBoot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.