java - 使用 JAX-RS 中的 @QueryParam 和 @DefaultValue 时为 "Annotation type not applicable to this kind of declaration"

标签 java jersey jax-rs

我收到以下编译错误:

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/

相关文章:

Java Android 滑动调整图像大小

java - 我应该如何使用 jax rs/jersey 从 JSONArray 输出 json?

java - JAX-RS:每个资源的选项

java - 将 Spring 属性占位符与 Jersey @Path 和 @ApplicationPath 一起使用

java - 保护 Jersey RESTful Web 服务

java - 使用 JAX-RS 客户端从 REST 服务下载文件

jax-rs - Servlet 3.0 和 JAX-RS

java - 如何查看arraylist java中的所有项目?

java - Jemos PODAM StackOverflowError

java - 调用 Calendar 类函数会产生 GC_CONCURRENT 消息