【问题标题】:How to return JSON objects from spring boot controller methods?如何从 Spring Boot 控制器方法返回 JSON 对象?
【发布时间】:2019-08-06 09:07:55
【问题描述】:

我正在使用 spring boot 以及 react js 和 postgresql。我正在尝试将表的行从 postgresql 打印到反应 js 页面。我在控制器方法中使用了 crud 存储库函数 findAll() 来获取列表。我的问题是,当我在 Spring Boot 控制台中打印列表时,它会打印列表,但在访问该 url 时会打印空对象的列表。

用户.java

@Entity
@Table(name="users")
public class User implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name="name")
    private String name;

    @Column(name="email")
    private String email;

    public User() {

    }

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    @Override
    public String toString() {
        return String.format("User[id=%d, name='%s', email='%s']",this.id,this.name,this.email);
    }
}

UserRepository.java

public interface UserRepository extends CrudRepository<User, Long>{


}

WebController.java

public class WebController {

    @Autowired
    private UserRepository repository;

    @GetMapping("home")
    public String home() {
        System.out.println("whaaat");
        return "hi ssup";
    }

    @GetMapping("/save")
    public String process() {

        repository.save(new User("vidhi","vd@gmail.com"));
        System.out.print("apple ");
        return "Done";
    }

    @GetMapping("findall")
    @ResponseBody
    public Collection<User> findAll() {
        System.out.println("cc");
        List<User> users = (List<User>) repository.findAll();
        System.out.println(users);
        return users;
    }
}

在启动时打印用户:[User[id=33, name='i', email='vd@gmail.com'], User[id=34, name='v', email='d@ gmail.com']

在 localhost:8080/findall: [{},{}]

这里出了什么问题?我很困惑,很长时间以来一直试图弄清楚这一点,这让我很头疼。

任何帮助都会很棒!

感谢您的宝贵时间。

【问题讨论】:

  • 用户有getter和setter吗?
  • org.springframework.data.repository.CrudRepository#findAll 方法为您提供 Iterable。你怎么能将 Iterable 转换为 List 而不会遇到 java.lang.ClassCastException?
  • @SureshMSidy 我没有遇到任何异常。
  • @SimonMartinelli 不,没有 getter 和 setter。他们必须吗?
  • @SimonMartinelli 天哪,非常感谢。只需添加 getter 和 setter ,一切都正常。我是个菜鸟,但你能告诉我原因吗?

标签: reactjs postgresql spring-boot react-router


【解决方案1】:

您必须将 getter 和 setter 添加到 User 类。

【讨论】:

    【解决方案2】:

    改成:

    @GetMapping("findall", produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<Collection<User>> findAll() {
        System.out.println("cc");
        List<User> users = (List<User>) repository.findAll();
        System.out.println(users);
        return ResponseEntity.ok(users);
    }
    

    【讨论】:

    • 嘿,谢谢,但这也行不通。它给出了错误 APPLICATION_JSON_VALUE 无法解决。
    • 如果无法解决,则说明您缺少关键依赖项。检查你的pom。你需要这个,可能还有 JPA &lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt; &lt;/dependency&gt;
    【解决方案3】:

    将您的存储库更改为:

    @Repository
    public interface UserRepository extends JpaRepository<User, Long>{
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2020-01-13
      • 2020-01-26
      • 2018-07-14
      • 1970-01-01
      相关资源
      最近更新 更多