4

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.

Should I override the findAll() method?

Thanks for the attention and time!

lcnicolau
  • 3,252
  • 4
  • 36
  • 53
KostasC
  • 1,076
  • 6
  • 20
  • 40
  • 1
    It all depends on how you transform the entities to JSON, and also probably on the code of the entity. It has nothing to do with the CrudRepository. – JB Nizet Nov 08 '14 at 16:15
  • Thanks for your answer! Do you mean like this [link](http://stackoverflow.com/questions/24936636/while-using-spring-data-rest-after-migrating-an-app-to-spring-boot-i-have-obser)? – KostasC Nov 08 '14 at 16:23
  • 1
    Maybe, maybe not. I don't know if you're using Spring-data-rest and if this answer applies to your application. – JB Nizet Nov 08 '14 at 16:27
  • Is this spring-data-rest? – Software Engineer Dec 04 '14 at 17:31

1 Answers1

4

I just add the following code on my Application class from the previous link:

@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!

bluish
  • 26,356
  • 27
  • 122
  • 180
KostasC
  • 1,076
  • 6
  • 20
  • 40
  • Take a look at [this](https://stackoverflow.com/a/49344988/4071001) in case you need to expose the identifiers for all entities, or only for those that extends or implements specific super class or interface. – lcnicolau Jul 05 '18 at 20:23