The easiest way is to return an Optional<MyObject> . Dropwizard automatically returns 404 when your result is Optional.absent() or Optional.empty() if you use the dropwizard-java8 package.
Just do:
@GET @Timed public Optional<MyObject> getMyObject(@QueryParam("id") Optional<String> id) { Optional<MyObject> myObjOptional = myDao.getMyObject(id) return myObjOptional; }
Obviously, you need to update your DAO according to the return of Optional.fromNullable(get(id)) for Guava or Optional.ofNullable(get(id)) for the Java8 package.
There is no need to play with custom Response objects if you do not want to return a custom status code beyond 200 and 404
source share