2014-11-08 58 views
3

我的问题是如何在Spring的CrudRepository的findAll方法中添加对象的id?我的意思是,我有以下对象:如何在Spring上获取CrudRepository对象的id

@Entity 
public class GameSet { 

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

    private String question; 
    ....... 

这里是我的仓库:

@Repository 
public interface GameSetRepository extends CrudRepository<GameSet, Long>{ 



} 

我的控制器:

@RequestMapping(value = "/test/getgamesets", method = RequestMethod.GET) 
    public @ResponseBody Collection<GameSet> getGameSets() { 
     return Lists.newArrayList(gamesets.findAll()); 
    } 

当我提出我的服务器我请求得到这个:

{ 
     "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 
    }, 

我w应该在请求结果中提取对象的id,以及对象的其他属性!我想要一些指南在哪里看!我需要覆盖方法findAll()?

感谢您的关注和时间!

+1

这一切都取决于你如何将实体转换为JSON,也可能取决于实体的代码。它与CrudRepository无关。 – 2014-11-08 16:15:53

+0

感谢您的回答!你的意思是这样[链接](http://stackoverflow.com/questions/24936636/while-using-spring-data-rest-after-migrating-an-app-to-spring-boot-i-have-obser )? – KostasC 2014-11-08 16:23:09

+1

也许,也许不是。我不知道你是否在使用Spring-data-rest,如果这个答案适用于你的应用程序。 – 2014-11-08 16:27:00

回答

3

我只是从一个链接添加以下代码在我的应用程序类:

@Configuration 
public static class RepositoryConfig extends RepositoryRestMvcConfiguration { 
    @Override 
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { 
     config.exposeIdsFor(GameSet.class); 
    } 
} 

和它的作品!谢谢JB Nizet让我清楚我的问题是什么!