Java 如何检索资源方法&x27;在泽西岛的MessageBodyWriter中有什么注释?

Java 如何检索资源方法&x27;在泽西岛的MessageBodyWriter中有什么注释?,java,annotations,jersey,exceptionmapper,Java,Annotations,Jersey,Exceptionmapper,泽西岛2.21 我有一个如下的资源文件 …… @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

泽西岛2.21

我有一个如下的资源文件

……
@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;
}
……
@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();
    }
}
为了将结果返回给客户端,我实现了一个自定义MessageBodyWriter,如下所示

……
@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;
}
……
@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();
    }
}
@产生(“应用程序/json”)
公共类MyRespWriter实现MessageBodyWriter{
@凌驾
公共布尔值是可写的(类aClass,类型类型,注释[]注释,MediaType MediaType){
返回类型==JsonResp.class;
}
@凌驾
公共长getSize(JsonResp JsonResp,类aClass,类型类型,注释[]注释,MediaType MediaType){
返回0;
}
@凌驾
public void writeTo(JsonResp JsonResp,类aClass,类型类型,注释[]注释,MediaType MediaType,MultivaluedMap MultivaluedMap,OutputStream OutputStream)抛出IOException,WebApplicationException{
//如果userReg()中没有异常,
//参数注释包含注释
//如岗位、路径、生产;
//但是如果userReg()中存在异常,
//参数注释不包含POST、Path和PRODUCTS;
//那么,有没有办法一直检索原始注释?
//JsonUtils是一个自定义java类。
String data=JsonUtils.toJsonString(jsonResp);
Writer osWriter=新的OutputStreamWriter(outputStream,“UTF-8”);
osWriter.write(数据);
osWriter.flush();
}
}
为了处理异常,我实现了一个ExceptionMapper,如下所示:

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();
    }
}
公共类MyExceptionMapper实现ExceptionMapper{
公众回应(例外e){
JsonResp result=新的JsonResp();
//错误
result.setResult(-1);
result.setErrMsg(“系统错误”);
返回Response.ok(result,MediaType.APPLICATION_JSON_TYPE).status(Response.status.ok).build();
}
}
现在,如果一切正常并且没有异常,代码执行路由器是
userReg()->MyRespWriter.writeTo()
MyRespWriter.writeTo()
的参数“annotations”包含方法
userReg()
的正确注释,例如
POST
Path
生成

但是如果
userReg()
抛出异常,则代码执行路由器是
userReg()->MyExceptionMapper.toResponse()->MyRespWriter.writeTo()
,方法
MyRespWriter.writeTo()的参数“annotations”没有方法
userReg()
的任何注释

我想知道,
MyRespWriter.writeTo()
有什么方法可以一直检索原始注释吗?

您可以注入,然后使用
ri.getResourceMethod()
获取
方法,然后调用
Method.getAnnotations()
获取注释

public class MyRespWriter implements MessageBodyWriter<JsonResp> {

    @Context
    ResourceInfo ri;

    ...

    Annotations[] annos = ri.getResourceMethod().getAnnotations();
公共类MyRespWriter实现MessageBodyWriter{
@上下文
ResourceInfo ri;
...
Annotations[]annos=ri.getResourceMethod().getAnnotations();

谢谢!这就是我想要的!