String Hateoas link to a void method
Asked Answered
O

2

7

I am working with Spring Hateoas for HAL standards in HTTP response. I have a HTTP DELETE method in my controller which returns nothing (void). And in the response for same entity I want to provide a link to delete a resource. I tried to do with following code but it gives error

Cannot resolve method linkTo(void)

    resource.add(linkTo(
            methodOn(DokumenteController.class)
                    .loeschenEinDokument(filenetDokumentZuordnung.getDokumentId()))
                            .withRel("download"));

Is there any way I can add a link to a method which returns void?

Onions answered 7/12, 2016 at 12:17 Comment(2)
See #30574931, could help, although the single answer hasn't been accepted nor upvoted...Powerboat
This is a very nice Github link to look at. The short answer is: it cannot be done directly. The answer provided by 'a better olive' is the best you can do. github.com/spring-projects/spring-hateoas/issues/1173Serval
G
15

Don't return void. Return ResponseEntity<Void> instead.

Chances are, that you have to set some headers anyway, even if you don't return a message body. Or you want to set a status code.

If your controller has an appropriate request mapping you can also do the following:

 resource.add(linkTo(DokumenteController.class)
             .slash(filenetDokumentZuordnung.getDokumentId())
             .withRel("download"));
Giles answered 7/12, 2016 at 13:29 Comment(1)
It might be worth reformatting the response to emphasize that there are two answers here, I found the 2nd to be a better fit for my case.Awlwort
R
3

I doubt it's possible to link to a DELETE route.

Hateoas is allowing you to discover your REST API, but a REST API means that in order to delete the document available at /document/42 you should call the same route, but with a DELETE method.

Basically, you don't have to make a link to your deletion method, because it is implicit that this is the way to delete your document.

Riehl answered 7/12, 2016 at 12:34 Comment(1)
It makes sense to have separate links for each CRUD operation to give the client information about if a specific operation is allowed or not. If the client receives a href with relation name 'deleteDocument', delete would be allowed. If not, it is not allowed.Moreville

© 2022 - 2024 — McMap. All rights reserved.