在 Java 中获取注解的参数值 [英] Get value of a parameter of an annotation in Java

查看:68
本文介绍了在 Java 中获取注解的参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个代码:

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

@GET
@Produces("text/html")
public String getHtml(@Context Request request, @Context HttpServletRequest requestss){
  ...
}

我正在使用 AspectJ 来捕获对 getHtml 方法的所有调用.我想在我的建议中将参数传递给 @Produces@Path,即 "/foo""text/html" 在这种情况下.我如何使用反射来做到这一点?

I am using AspectJ to catch all calls to getHtml method. I would like to get parameters passed to @Produces and to @Path in my advice, i.e. "/foo" and "text/html" in this case. How can I do it using reflection ?

推荐答案

获取@Path 参数的值:

String path = Hello.class.getAnnotation(Path.class).value();

同样,一旦你掌握了Method getHtml

Method m = Hello.class.getMethod("getHtml", ..);
String mime = m.getAnnotation(Produces.class).value;

这篇关于在 Java 中获取注解的参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆