阅读 321

SpringBoot返回Json对象报错(返回对象为空{})

本文主要介绍介绍了SpringBoot返回Json对象报错(返回对象为空{}),文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

目录
  • 1 需求描述

  • 2 代码展示

  • 3 原因分析

  • 4 解决方案

  • 5 效果展示

  • 6 结束语

1 需求描述

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

在这里插入图片描述

接受结果:

在这里插入图片描述

2 代码展示

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

在这里插入图片描述

接下来是具体的文件内容首先呢我们展示一下User.java文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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文件的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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,冲冲冲!

到此这篇关于SpringBoot返回Json对象报错(返回对象为空{})的文章就介绍到这了

原文链接:https://blog.csdn.net/qq_41010280/article/details/122589691


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐