我收到以下编译错误:
Error(35,13): annotation type not applicable to this kind of declaration
Error(36,13): annotation type not applicable to this kind of declaration
Error(38,13): annotation type not applicable to this kind of declaration
Error(39,13): annotation type not applicable to this kind of declaration
Error(41,13): annotation type not applicable to this kind of declaration
Error(42,13): annotation type not applicable to this kind of declaration
所有这些错误都是来自 Jersey 的 @ParamValue
和 @DefaultValue
注释。我在互联网上看到了很多例子,他们都说 Jersey 允许字符串类和所有包装类。我不明白为什么它在这里不起作用。
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("/sracows")
public class sracoWebService {
public sracoWebService() {
super();
}
@GET
@Path("/empdata")
public String getEmployeeData() throws Exception {
try {
@DefaultValue("nationality")
@QueryParam("nationality")
String nationality;
@DefaultValue("experience")
@QueryParam("experience")
String experience;
@DefaultValue("empid")
@QueryParam("empid")
String empid;
} catch(Exception e){
e.printStackTrace();
}
}
}
最佳答案
两者@QueryParam
和 @DefaultValue
注释只能放置在资源方法参数、资源类字段或资源类bean 属性上。
您的注释位于局部变量上,这就是您出现编译错误的原因。
你可以...
@Path("/foo")
public class MyResourceClass {
@GET
@Path("/bar")
public String myResourceMethod(
@QueryParam("nationality") @DefaultValue("nationality") String nationality,
@QueryParam("experience") @DefaultValue("experience") String experience,
@QueryParam("empid") @DefaultValue("empid") String empid) {
...
}
}
你可以...
@Path("/foo")
public class MyResourceClass {
@QueryParam("nationality")
@DefaultValue("nationality")
private String nationality;
@QueryParam("experience")
@DefaultValue("experience")
private String experience;
@QueryParam("empid")
@DefaultValue("empid")
private String empid;
@GET
@Path("/bar")
public String myResourceMethod() {
...
}
}
你可以...
public class ParameterAggregator {
@QueryParam("nationality")
@DefaultValue("nationality")
private String nationality;
@QueryParam("experience")
@DefaultValue("experience")
private String experience;
@QueryParam("empid")
@DefaultValue("empid")
private String empid;
// Getters and setters
}
@Path("/foo")
public class MyResourceClass {
@GET
@Path("/bar")
public String myResourceMethod(@BeanParam ParameterAggregator params) {
...
}
}
关于java - 使用 JAX-RS 中的 @QueryParam 和 @DefaultValue 时为 "Annotation type not applicable to this kind of declaration",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46037211/