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": "[email protected]",
        "createdAt": "2016-04-25 11:49"
    },
    {
      ...

Payload without my custom controller looks like this:

{
    "_embedded": {
        "users": [
            {
                "username": "mark",
                "password": "mark123",
                "email": "[email protected]",
                "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?

12
  • 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 Commented Apr 25, 2016 at 12:29
  • @kakashihatake thanks for suggestion but it's not working. The result is same as before. Commented Apr 25, 2016 at 12:52
  • did you try request with this path /people/getall ? Commented Apr 25, 2016 at 12:54
  • Yes, I did! But still no success. Commented Apr 25, 2016 at 13:00
  • then please add stack trace in question Commented Apr 25, 2016 at 13:01

1 Answer 1

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": "[email protected]",
        "createdAt": "2016-04-25 18:
        "links": [
            {
                "rel": "self",
                "href": "http://localhost:8080/users/571e44ecbdab7b1ffc668f02"
            },
            {
                "rel": "users",
                "href": "http://localhost:8080/users"
            }
        ]
    },
    {
      ....
Sign up to request clarification or add additional context in comments.

1 Comment

Still has significant differences with the HATEOAS response from the repo ... do you have additional information about this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.