Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return HTTP error from RESTeasy interface

Is it possible to return a HTTP error from a RESTeasy interface? I am currently using chained web-filters for this but I want to know if it is possible straight from the interface...

Example sudo-code:

@Path("/foo")
public class FooBar {

    @GET
    @Path("/bar")
    @Produces("application/json")
    public Object testMethod(@HeaderParam("var_1") @DefaultValue("") String var1,
                             @HeaderParam("var_2") @DefaultValue("") String var2 {

        if (var1.equals(var2)) {
            return "All Good";
        } else {
            return HTTP error 403;
        }
    }
}
like image 821
travega Avatar asked Mar 14 '12 00:03

travega


1 Answers

Found the solution and it's very simple:

throw new WebApplicationException();

So:

@Path("/foo")
public class FooBar {

    @GET
    @Path("/bar")
    @Produces("application/json")
    public Object testMethod(@HeaderParam("var_1") @DefaultValue("") String var1,
                             @HeaderParam("var_2") @DefaultValue("") String var2 {

        if (var1.equals(var2)) {
            return "All Good";
        } else {
            throw new WebApplicationException(HttpURLConnection.HTTP_FORBIDDEN);
        }
    }
}
like image 166
travega Avatar answered Sep 27 '22 20:09

travega