admin管理员组文章数量:1391955
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);
}
本文标签: How to convert URI to object in custom springdatarest controllerStack Overflow
版权声明:本文标题:How to convert URI to object in custom spring-data-rest controller? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744743041a2622722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论