[英]How to pass name value annotation in Java method?
The annotation class Get.java 注释类Get.java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Get {
String value();
}
The annotation class Field.java 注释类Field.java
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@interface Field {
String value();
}
The interface class GetInterface.java 接口类GetInterface.java
public interface GetInterface {
@Get("/group/user?")
void getUser(@Field("id") String id);
}
I want to use this GetInterface in the following, how can I get the annotation name so I can use that as the parameter attribute to construct a get url with query parameters 我想在下面使用此GetInterface,如何获取批注名称,以便可以将其用作参数属性来构造具有查询参数的get url
public class Request implements GetInterface {
@Override
public getUser(String id) {
String getPath = "/group/user?"; //How can I get this path from the annotation?
String name = "how can I get the name attribute 'id'????";
String value = id; //this will be 123 in this example
//with the name an value, I can then construct a url like this
//https://www.foo.com/group/user?id=123
}
public static void main(String[] args) {
getUser("123);
}
}
Do the following: 请执行下列操作:
Method m = GetInterface.class.getMethod("getUser");
Get getAnnotation = m.getAnnotation(Get.class)
Field fieldAnnotation = m.getParameters()[0].getAnnotation(Field.class)
String path = getAnnotation.value();
String fieldName = fieldAnnotation.value();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.