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