温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot返回Json对象报错怎么办

发布时间:2022-03-04 10:17:21 来源:亿速云 阅读:321 作者:小新 栏目:开发技术

小编给大家分享一下SpringBoot返回Json对象报错怎么办,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

1 需求描述

我们现在要干一个什么事情呢,我们要在浏览器输入一个请求地址,然后我们的后端就给我返回一个User对象即可,并且我希望以Json的格式返回。这个需求很明确,我们先直观的展示一下效果。
发送请求:

SpringBoot返回Json对象报错怎么办

接受结果:

SpringBoot返回Json对象报错怎么办

2 代码展示

行了,明确了需求我们开始整活儿。首先我们老规矩还是先展示一下目录结构(其中标红的文件使我们今天要用到的):

SpringBoot返回Json对象报错怎么办

接下来是具体的文件内容首先呢我们展示一下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");
    }
}

好了齐活了,我们来测试一下:

SpringBoot返回Json对象报错怎么办

SpringBoot返回Json对象报错怎么办

没想到吧结果却是这个鬼样子!没返回!!!为啥呢?

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 效果展示

这次就行了哦

SpringBoot返回Json对象报错怎么办

以上是“SpringBoot返回Json对象报错怎么办”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI