Get value of a parameter of an annotation in Java

前端 未结 2 1389
梦谈多话
梦谈多话 2020-12-06 09:19

So I\'ve got a code:

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

@GET
@Produces(\"text/html\")
public String getHtml(@Context Request request, @Context HttpServlet         


        
2条回答
  •  悲哀的现实
    2020-12-06 09:35

    To get value of the @Path parameter:

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

    Similarly, Once you have hold of Method getHtml

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

提交回复
热议问题