我有一个REST端点,如下所示。

@Path("/consumers")
@Produces("application/x.com.abc.pqr.audit.v2+json")
@Consumes("application/x.com.abc.pqr.audit.v2+json")
public interface ConsumerEndpoint {

    @GET
    @Path("paged")
    Page<Module> getConsumersOfDependencyByPage(@BeanParam ConsumerQueryParams params);

}


如您在上面看到的,我正在使用@BeanParam映射从前端传递的查询参数。

ConsumerQueryParams类如下所示。

public class ConsumerQueryParams implements Serializable{
    private static final long serialVersionUID = 6440255704974023223L;

    @QueryParam("pageNum") @DefaultValue("1") private int pageNum;
    @QueryParam("pageSize") @DefaultValue("25") private int pageSize;
    @QueryParam("groupId") private String groupId;
    @QueryParam("artifactId") private String artifactId;
    @QueryParam("version") private String version;
    @QueryParam("groupIdFilter") private String groupIdFilter;
    @QueryParam("artifactIdFilter") private String artifactIdFilter;
    @QueryParam("versionFilter") private String versionFilter;

    public ConsumerQueryParams() {
    }

    private ConsumerQueryParams(Builder builder) {
        this.pageNum = builder.pageNum;
        this.pageSize = builder.pageSize;
        this.groupId = builder.groupId;
        this.artifactId = builder.artifactId;
        this.version = builder.version;
        this.groupIdFilter = builder.groupIdFilter;
        this.artifactIdFilter = builder.artifactIdFilter;
        this.versionFilter = builder.versionFilter;
    }

    public int getPageNum() {
        return pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public String getGroupId() {
        return groupId;
    }

    public String getArtifactId() {
        return artifactId;
    }

    public String getVersion() {
        return version;
    }

    public String getGroupIdFilter() {
        return groupIdFilter;
    }

    public String getArtifactIdFilter() {
        return artifactIdFilter;
    }

    public String getVersionFilter() {
        return versionFilter;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj)
            return true;
        if(!(obj instanceof ConsumerQueryParams))
            return false;
        ConsumerQueryParams other = (ConsumerQueryParams) obj;
        return Objects.equals(pageNum, other.pageNum) &&
                Objects.equals(pageSize, other.pageSize) &&
                Objects.equals(groupId, other.groupId) &&
                Objects.equals(artifactId, other.artifactId) &&
                Objects.equals(version, other.version) &&
                Objects.equals(groupIdFilter, other.groupIdFilter) &&
                Objects.equals(artifactIdFilter, other.artifactIdFilter) &&
                Objects.equals(versionFilter, other.versionFilter);
    }

    @Override
    public int hashCode() {
        return Objects.hash(pageNum, pageSize, groupId, artifactId, version, groupIdFilter, artifactIdFilter, versionFilter);
    }

    public static class Builder {
        private int pageNum;
        private int pageSize;
        private String groupId;
        private String artifactId;
        private String version;
        private String groupIdFilter;
        private String artifactIdFilter;
        private String versionFilter;

        public Builder(int pageNum, int pageSize, String groupId, String artifactId) {
            Preconditions.checkArgument(pageNum > 0, "pageNum must be greater than 0.");
            Preconditions.checkArgument(pageSize > 0, "pageSize must be greater than 0.");
            Preconditions.checkNotNull(groupId, "groupId is null");
            Preconditions.checkNotNull(artifactId, "artifactId is null");

            this.pageNum = pageNum;
            this.pageSize = pageSize;
            this.groupId = groupId;
            this.artifactId = artifactId;
        }

        public Builder setVersion(String version) {
            this.version = version;
            return this;
        }

        public Builder setGroupIdFilter(String groupIdFilter) {
            this.groupIdFilter = groupIdFilter;
            return this;
        }

        public Builder setArtifactIdFilter(String artifactIdFilter) {
            this.artifactIdFilter = artifactIdFilter;
            return this;
        }

        public Builder setVersionFilter(String versionFilter) {
            this.versionFilter = versionFilter;
            return this;
        }

        public ConsumerQueryParams build() {
            return new ConsumerQueryParams(this);
        }
    }
}


您可以看到我正在使用Builder模式设置变量。
我正在使用下面的URL访问上面指定的端点。

http://localhost:8080/rest/api/consumers/paged?groupId=org.slf4j&artifactId=slf4j-api&groupIdFilter=sdlc


一切正常。我能够成功获取Postman上的数据。
现在,我有一个需求,我需要将groupIdFilter类中的artifactIdFilterversionFilterConsumerQueryParams查询参数分别重命名为consumerGroupIdFilterconsumerArtifactIdFilterconsumerVersionFilter
更改ConsumerQueryParams类中的变量名称后,如下所示:

public class ConsumerQueryParams implements Serializable{
    private static final long serialVersionUID = 6440255704974023223L;

    @QueryParam("pageNum") @DefaultValue("1") private int pageNum;
    @QueryParam("pageSize") @DefaultValue("25") private int pageSize;
    @QueryParam("groupId") private String groupId;
    @QueryParam("artifactId") private String artifactId;
    @QueryParam("version") private String version;
    @QueryParam("groupIdFilter") private String consumerGroupIdFilter;
    @QueryParam("artifactIdFilter") private String consumerArtifactIdFilter;
    @QueryParam("versionFilter") private String consumerVersionFilter;

    public ConsumerQueryParams() {
    }

    private ConsumerQueryParams(Builder builder) {
        this.pageNum = builder.pageNum;
        this.pageSize = builder.pageSize;
        this.groupId = builder.groupId;
        this.artifactId = builder.artifactId;
        this.version = builder.version;
        this.consumerGroupIdFilter = builder.consumerGroupIdFilter;
        this.consumerArtifactIdFilter = builder.consumerArtifactIdFilter;
        this.consumerVersionFilter = builder.consumerVersionFilter;
    }

    public int getPageNum() {
        return pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public String getGroupId() {
        return groupId;
    }

    public String getArtifactId() {
        return artifactId;
    }

    public String getVersion() {
        return version;
    }

    public String getConsumerGroupIdFilter() {
        return consumerGroupIdFilter;
    }

    public String getConsumerArtifactIdFilter() {
        return consumerArtifactIdFilter;
    }

    public String getConsumerVersionFilter() {
        return consumerVersionFilter;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj)
            return true;
        if(!(obj instanceof ConsumerQueryParams))
            return false;
        ConsumerQueryParams other = (ConsumerQueryParams) obj;
        return Objects.equals(pageNum, other.pageNum) &&
                Objects.equals(pageSize, other.pageSize) &&
                Objects.equals(groupId, other.groupId) &&
                Objects.equals(artifactId, other.artifactId) &&
                Objects.equals(version, other.version) &&
                Objects.equals(consumerGroupIdFilter, other.consumerGroupIdFilter) &&
                Objects.equals(consumerArtifactIdFilter, other.consumerArtifactIdFilter) &&
                Objects.equals(consumerVersionFilter, other.consumerVersionFilter);
    }

    @Override
    public int hashCode() {
        return Objects.hash(pageNum, pageSize, groupId, artifactId, version, consumerGroupIdFilter, consumerArtifactIdFilter, consumerVersionFilter);
    }

    public static class Builder {
        private int pageNum;
        private int pageSize;
        private String groupId;
        private String artifactId;
        private String version;
        private String consumerGroupIdFilter;
        private String consumerArtifactIdFilter;
        private String consumerVersionFilter;

        public Builder(int pageNum, int pageSize, String groupId, String artifactId) {
            Preconditions.checkArgument(pageNum > 0, "pageNum must be greater than 0.");
            Preconditions.checkArgument(pageSize > 0, "pageSize must be greater than 0.");
            Preconditions.checkNotNull(groupId, "groupId is null");
            Preconditions.checkNotNull(artifactId, "artifactId is null");

            this.pageNum = pageNum;
            this.pageSize = pageSize;
            this.groupId = groupId;
            this.artifactId = artifactId;
        }

        public Builder setVersion(String version) {
            this.version = version;
            return this;
        }

        public Builder setConsumerGroupIdFilter(String consumerGroupIdFilter) {
            this.consumerGroupIdFilter = consumerGroupIdFilter;
            return this;
        }

        public Builder setConsumerArtifactIdFilter(String consumerArtifactIdFilter) {
            this.consumerArtifactIdFilter = consumerArtifactIdFilter;
            return this;
        }

        public Builder setConsumerVersionFilter(String consumerVersionFilter) {
            this.consumerVersionFilter = consumerVersionFilter;
            return this;
        }

        public ConsumerQueryParams build() {
            return new ConsumerQueryParams(this);
        }
    }
}


现在,我尝试使用url访问相同的端点:

http://localhost:8080/rest/api/consumers/paged?groupId=org.slf4j&artifactId=slf4j-api&consumerGroupIdFilter=sdlc


但这是行不通的。 URL中的consumerGroupIdFilter查询参数未映射到consumerGroupIdFilter对象的ConsumerQueryParams变量,而groupIdartifactId被映射。
我不确定为什么会这样。据我所知,ConsumerQueryParams类具有正确的代码。我所做的就是更改变量名称并更新了Builder类中的getter和setter。
有人能帮我一下吗。

最佳答案

问题在于网址具有新名称,而注释具有旧名称

@QueryParam("groupIdFilter")

consumerGroupIdFilter

09-28 12:34