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?