最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to convert URI to object in custom spring-data-rest controller? - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

All 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);
    }
发布评论

评论列表(0)

  1. 暂无评论