javaannotationsjerseyexceptionmapper

How to retrieve resource method's Annoations in MessageBodyWriter in Jersey?


jersey 2.21.

I have a resource file like below

……
@POST
@Path("/userReg")
@Produces("application/json;charset=UTF-8")
public JsonResp userReg(UserRegReq userRegReq) throws LoginNameExists {
    HttpHeaderUtils.parseHeaders(userRegReq, headers);

    //JsonResp is a custom java class.
    JsonResp result = new JsonResp();

    //will throw LoginNameExists
    User user = userManager.register(userRegReq.getLoginName(), userRegReq.getPassword());

    //success
    result.setResult(0);
result.setData(user.getId);

    return result;
}
……

To return the result to client, I implement a custom MessageBodyWriter like below

@Produces("application/json")
public class MyRespWriter implements MessageBodyWriter<JsonResp> {

    @Override
    public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return type == JsonResp.class;
    }

    @Override
    public long getSize(JsonResp jsonResp, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return 0;
    }

    @Override
public void writeTo(JsonResp jsonResp, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
        //if these no exception in userReg(), 
        //the parameter annotations contains the annotations 
        //such as POST, Path, Produces;
        //but if there is an exception in userReg(), 
        //the parameter annotations contains none of POST, Path, Produces; 
        //So, is there any way to retrieve the original annotations all along?


        //JsonUtils is a custom java class.
        String data = JsonUtils.toJsonString(jsonResp);
        Writer osWriter = new OutputStreamWriter(outputStream, "UTF-8");
        osWriter.write(data);
        osWriter.flush();
    }
}

And to handle the exceptions, I implement a ExceptionMapper like this:

public class MyExceptionMapper implements ExceptionMapper<Exception> {

    public Response toResponse(Exception e) {

        JsonResp result = new JsonResp();

        //error
        result.setResult(-1);
        result.setErrMsg("System error.");

        return Response.ok(result, MediaType.APPLICATION_JSON_TYPE).status(Response.Status.OK).build();
    }
}

Now, if everything is ok and there’s no exception, the code execution router is userReg() -> MyRespWriter.writeTo(), the parameter "annotations" of MyRespWriter.writeTo() contains the correct annotations of method userReg(), such as POST, Path, Produces.

But if userReg() throws exception, the code execution router is userReg() -> MyExceptionMapper.toResponse() -> MyRespWriter.writeTo(), the parameter "annotations" of method MyRespWriter.writeTo() has none of the annotations of method userReg().

I want to know, is there any way that MyRespWriter.writeTo() can retrieve the original annotations all along?


Solution

  • You can inject ResourceInfo, then get the Method with ri.getResourceMethod(), then call method.getAnnotations() to get the annotations.

    public class MyRespWriter implements MessageBodyWriter<JsonResp> {
    
        @Context
        ResourceInfo ri;
    
        ...
    
        Annotations[] annos = ri.getResourceMethod().getAnnotations();