简体   繁体   English

Spring 启动 JSON 响应

[英]Spring boot JSON response

Here's my code这是我的代码

@RequestMapping("/bookList") 
public List<Books> list() {
    return bookService.booksList();
}

public class Books {
   private String author;
   private String isbn;
   private String title;
}

Current response当前响应

[["TOM",123456,"ABC"],["JANE",789000,"CDE"]]

Expected response预期回应

"Books": [
    {"author": "TOM", "isbn": "123456", "title": "ABC"},
    {"author": "JANE", "isbn": "789000", "title": "CDE"}
]

What is the code change I need to make?我需要进行哪些代码更改?

Your List<Books> is correctly returned as JSON.您的List<Books>正确返回为 JSON。 Your option would be to wrap it into another object:您的选择是将其包装到另一个对象中:

public class BookList {
    public List<Books> books;
}

and return a BookList instead of List<Books> .并返回BookList而不是List<Books>

But to be honest, I would not recommend you do do that.但老实说,我不建议你这样做。 It's not very restful.它不是很安静。

session.createQuery returns Object[]. session.createQuery 返回 Object[]。 I used Criteria query with projections inside my DAOIMPL class and I got the expected output.我在 DAOIMPL 类中使用了带有投影的 Criteria 查询,并得到了预期的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM