1

I use MongoRepository in Spring boot data rest and it is working just fine without implementing my own controller. But I want to put "Register Date" in my newly created objects and default implementation is not supporting that. I need to implement my own custom controller to put extra fields in every new objects. The problem is that HATEOAS stop working when I implement my own controller.

Repository class:

@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends MongoRepository<User, String> {
}

Controller class:

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    UserRepository repository;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<User>> getAll() {
        List<User> list = repository.findAll();
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

Payload with this custom controller looks like this:

[
    {
        "id": "571de80ebdabf25dd6cdfb73",
        "username": "mark",
        "password": "mark123",
        "email": "mark@gmail.com",
        "createdAt": "2016-04-25 11:49"
    },
    {
      ...

Payload without my custom controller looks like this:

{
    "_embedded": {
        "users": [
            {
                "username": "mark",
                "password": "mark123",
                "email": "mark@gmail.com",
                "createdAt": "2016-04-25 11:49",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/users/571de80ebdabf25dd6cdfb73"
                    },
                    "user": {
                        "href": "http://localhost:8080/users/571de80ebdabf25dd6cdfb73"
                    }
                }
            },
            {
             .....

I tried to use @RepositoryRestController instead of @RestController but it didn't help. I wonder if there is another way to put "register date" in newly created objects without implementing own custom controller? If not, what can I do HATEOAS to work again?

oxyt
  • 1,816
  • 3
  • 23
  • 33
  • you give same path controller and repository rest source ad just add spesific path in your custom controller fore example replace your getAll() method request mapping with this @RequestMapping(method = RequestMethod.GET , value = "/getall") and try after – kakashi hatake Apr 25 '16 at 12:29
  • @kakashihatake thanks for suggestion but it's not working. The result is same as before. – oxyt Apr 25 '16 at 12:52
  • did you try request with this path /people/getall ? – kakashi hatake Apr 25 '16 at 12:54
  • Yes, I did! But still no success. – oxyt Apr 25 '16 at 13:00
  • then please add stack trace in question – kakashi hatake Apr 25 '16 at 13:01
  • What do you mean by _"HATEOAS stop working"_? You return a simple list, not a HATEOAS resource anyway. – a better oliver Apr 25 '16 at 13:02
  • @oxyt take a look here: http://stackoverflow.com/q/33571920/5873923 – Marc Tarin Apr 25 '16 at 13:18
  • @zeroflagL `spring-boot-starter-data-rest` gives me `HATEOAS` out of box for all my endpoints if I don't use a custom controller but if I do, than I get no more HATEOAS – oxyt Apr 25 '16 at 13:50
  • @zeroflagL I updated my question. You can see the payload with both cases. – oxyt Apr 25 '16 at 13:59
  • @MarcTarin I checked that post but that is a bit complex for my understanding. I couldn't make that work. :) – oxyt Apr 25 '16 at 14:01
  • _"gives me HATEOAS out of box for all my endpoints if I don't use a custom controller"_ Custom controllers ARE all of your endpoints. `UserRepository` is not an endpoint for the web. It's a repository for which an endpoint will be created. Spring Data REST uses Spring HATEOAS and the endpoints it creates don't return simple collections or objects. They return instances of `ResourceSupport`. – a better oliver Apr 26 '16 at 07:42
  • @zeroflagL thank you for the explanation. I will have a clooser look to `ResourceSupport` again. – oxyt Apr 26 '16 at 09:13

1 Answers1

0

I solved my problem thanks to comments which gave me a perspective :)

1 - Extended User class with ResourceSupport. (Note: Do not use just id for userId because ResourceSupport needs getId() method.)

public class User extends ResourceSupport {

    @Id
    private String userId;

2 - Updated my controller class as followed

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;

@RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<User>> getAll() {
        List<User> list = repository.findAll();
        for(User user : list) {
            Link self = linkTo(UserController.class).slash(user.getUserId()).withSelfRel();
            Link users = linkTo(UserController.class).slash(user.getId()).withRel("users");
            user.add(self);
            user.add(users);
        }
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

Now, my payloads look like this:

[
    {
        "userId": "571e44ecbdab7b1ffc668f02",
        "username": "newton",
        "password": "gravity",
        "email": "nevton@gmail.com",
        "createdAt": "2016-04-25 18:
        "links": [
            {
                "rel": "self",
                "href": "http://localhost:8080/users/571e44ecbdab7b1ffc668f02"
            },
            {
                "rel": "users",
                "href": "http://localhost:8080/users"
            }
        ]
    },
    {
      ....
oxyt
  • 1,816
  • 3
  • 23
  • 33
  • 2
    Still has significant differences with the HATEOAS response from the repo ... do you have additional information about this? – Rafael Mar 30 '17 at 11:01