[英]How to include in serialization the id of the recovered objects through the `findAll()` method of CrudRepository in Spring?
I have the following entity class: 我有以下实体类:
@Entity
public class GameSet {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String question;
.......
}
Here is my Repository: 这是我的存储库:
@Repository
public interface GameSetRepository extends CrudRepository<GameSet, Long> { }
Here is the relevant part of my Controller: 这是我的控制器的相关部分:
...
@RequestMapping(value = "/test/getgamesets", method = RequestMethod.GET)
public @ResponseBody Collection<GameSet> getGameSets() {
return Lists.newArrayList(gamesets.findAll());
}
...
And here is the server response: 这是服务器响应:
{
"question": "Choose one of the following, which is wrong.",
"title1": "MovieA",
"title2": "MovieB",
"title3": "MovieC",
"title4": "MovieD",
"wrong": 1,
"explain": "I don't know why this is wrong.",
"rates": 0,
"rate": 0
}
I would like to get the id of the object in the request result, in addition to the other properties. 除了其他属性之外,我想在请求结果中获取对象的id。
Should I override the findAll()
method? 我应该覆盖
findAll()
方法吗?
Thanks for the attention and time! 感谢您的关注和时间!
I just add the following code on my Application class from the previous link: 我只是在上一个链接的Application类中添加以下代码:
@Configuration
public static class RepositoryConfig extends RepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(GameSet.class);
}
}
And it works!!! 它的工作原理! Thanks JB Nizet to make me see clear what was my problem!
感谢JB Nizet让我看清楚我的问题是什么!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.