I use spring-data-rest to publish this entity, among others
@Entity
public class Activity {
@Id
private Long id;
@ManyToOne
private Customer customer;
}
Referenced entities such as customers are not displayed as objects, but as HATEOAS links.
GET http://localhost/api/activities/375835
{
"id": 375835,
"_links": {
"self": {
"href": "http://localhost/api/activities/375835"
},
"activity": {
"href": "http://localhost/api/activities/375835"
},
"customer": {
"href": "http://localhost/api/activities/375835/customer"
}
}
}
If i want to change the customer i can submit the URI to the new resource instead of the object itself and spring-data-rest will replace it with the corresponding object.
PATCH http://localhost/api/activities/375835
{
"customer" : "http://localhost:80/api/customers/61"
}
This works perfectly fine.
Some REST methods require custom logic, for this I use @BasePathAwareController as suggested in numerous posts.
@BasePathAwareController(path = "/activities")
public class ActivityController extends _BaseController<Activity, Long>
{
@RequestMapping(method = { RequestMethod.PUT }, path = "/{pos}", consumes = {MediaType.APPLICATION_JSON_VALUE, "application/hal+json"})
public ResponseEntity<Activity> edit(@PathVariable("pos") long pos, @RequestBody Activity activity) {
// custom logic...
Activity updated = repository.save(activity);
return ResponseEntity.ok(updated);
}
}
This also works fine.
What does not work is the convertion of the customer URI to the customer object.
What am I supposed to do to make this work? Ideally I want to reuse the code spring-data-rest uses somewhere in its depths. There must be some kind of HATEOAS<->Object jackson json mapper or something like that but i cant find it.
I dont want to use projections or DTOs, I just want my custom logic surrounded by the default spring-boot-rest behavior.
Thank you very much in advance.
I use spring-data-rest to publish this entity, among others
@Entity
public class Activity {
@Id
private Long id;
@ManyToOne
private Customer customer;
}
Referenced entities such as customers are not displayed as objects, but as HATEOAS links.
GET http://localhost/api/activities/375835
{
"id": 375835,
"_links": {
"self": {
"href": "http://localhost/api/activities/375835"
},
"activity": {
"href": "http://localhost/api/activities/375835"
},
"customer": {
"href": "http://localhost/api/activities/375835/customer"
}
}
}
If i want to change the customer i can submit the URI to the new resource instead of the object itself and spring-data-rest will replace it with the corresponding object.
PATCH http://localhost/api/activities/375835
{
"customer" : "http://localhost:80/api/customers/61"
}
This works perfectly fine.
Some REST methods require custom logic, for this I use @BasePathAwareController as suggested in numerous posts.
@BasePathAwareController(path = "/activities")
public class ActivityController extends _BaseController<Activity, Long>
{
@RequestMapping(method = { RequestMethod.PUT }, path = "/{pos}", consumes = {MediaType.APPLICATION_JSON_VALUE, "application/hal+json"})
public ResponseEntity<Activity> edit(@PathVariable("pos") long pos, @RequestBody Activity activity) {
// custom logic...
Activity updated = repository.save(activity);
return ResponseEntity.ok(updated);
}
}
This also works fine.
What does not work is the convertion of the customer URI to the customer object.
What am I supposed to do to make this work? Ideally I want to reuse the code spring-data-rest uses somewhere in its depths. There must be some kind of HATEOAS<->Object jackson json mapper or something like that but i cant find it.
I dont want to use projections or DTOs, I just want my custom logic surrounded by the default spring-boot-rest behavior.
Thank you very much in advance.
Share Improve this question asked Mar 12 at 15:48 MeiniMeini 773 silver badges19 bronze badges1 Answer
Reset to default 0All I have to do is wrap the entity in .springframework.hateoas.EntityModel and the URI will be perfectly converted
@RequestMapping(method = { RequestMethod.PUT }, path = "/{pos}", consumes = {MediaType.APPLICATION_JSON_VALUE, "application/hal+json"})
public ResponseEntity<Activity> edit(@PathVariable("pos") long pos, @RequestBody EntityModel<Activity> activity) {
// custom logic...
Activity updated = repository.save(activity);
return ResponseEntity.ok(updated);
}