Panache + RESTEasy handling Optional

276 views
Skip to first unread message

Antonio Goncalves

unread,
Apr 13, 2021, 12:04:00 AM4/13/21
to Quarkus Development mailing list
Hi,

I am using Panache + RESTEasy and I got surprised by the way it handles returning an `Optional<MyEntity>` [1]. If the optional if not present, it returns a 200. I was expecting at least a 204 (like when returning a non optional) :
@GET
@Path("/without/{id}")
public Book findBookByIdWithoutOptional(@PathParam("id") Long id) {
// 204 if not found
return Book.findById(id);
}

@GET
@Path("/with/{id}")
public Optional<Book> findBookByIdWithOptiona(@PathParam("id") Long id) {
// 200 if not found
return Book.findByIdOptional(id);
}

@GET
@Path("/response/{id}")
public Response findBookByIdWithResponse(@PathParam("id") Long id) {
return Book
.findByIdOptional(id)
.map(book -> Response.ok(book).build())
.orElse(Response.status(Response.Status.NOT_FOUND).build());
}

So the only way to return a 404 is return a Response and do it programmatically.

Is this the way it is supposed to work? I know that JAX-RS 2.1 does not handle Optional, but I was wondering if RESTEasy was doing something more than the spec in supporting Optional.

Thanks
Antonio

[1] https://github.com/agoncal/agoncal-sample-quarkus/tree/master/07-PanacheOptional

--
Antonio Goncalves 
Contractor, Java Champion and Pluralsight author

Blog | Twitter | LinkedIn Author Pluralsight | Devoxx France | Voxxed Microservices

Loïc MATHIEU

unread,
Apr 13, 2021, 4:31:02 AM4/13/21
to Antonio Goncalves, Quarkus Development mailing list
Hi,

I usually code something like this as a 404 will be sent when throwing a NotFoundException, I found it more self explanatory.

@GET
@Path("/response/{id}")
public Book findBookById(@PathParam("id") Long id) {
return Book
.findByIdOptional(id)
.orElseThrow(() -> new NotFoundException());
}

--
You received this message because you are subscribed to the Google Groups "Quarkus Development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quarkus-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/CAKawoObOp%2B-WBiJJzt_NDoXhrRV-OeTVw3DQbSSnCPBEV4ZEdQ%40mail.gmail.com.

Stephane Epardaud

unread,
Apr 13, 2021, 4:36:38 AM4/13/21
to Antonio Goncalves, Quarkus Development mailing list
IMO this is at least a usability bug. You should file it for RESTEasy (in their JIRA) and check if RESTEasy Reactive has the same issue and if yes file it in Quarkus.

Thanks!

--
You received this message because you are subscribed to the Google Groups "Quarkus Development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quarkus-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/CAKawoObOp%2B-WBiJJzt_NDoXhrRV-OeTVw3DQbSSnCPBEV4ZEdQ%40mail.gmail.com.


--
Stéphane Épardaud
Reply all
Reply to author
Forward
0 new messages