Search code examples
javarestjax-rsresteasybuilder

Query Parameters are not getting mapped


I have a REST endpoint as shown below.

@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);

}

As you can see above, I am using @BeanParam to map the query parameters passed from the front end side.

The ConsumerQueryParams class is shown below.

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);
        }
    }
}

You can see that I am using the Builder pattern to set the variables. I am using the below url to access the above specified endpoint.

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

Everything works fine. I am able to get the data on Postman successfully. Now I have a requirement where I need to rename the groupIdFilter, artifactIdFilter, versionFilter query params in ConsumerQueryParams class to consumerGroupIdFilter, consumerArtifactIdFilter and consumerVersionFilter respectively.
After changing the variable names in ConsumerQueryParams class, it looks like this:

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);
        }
    }
}

Now I am trying to access the same endpoint with the url:

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

But this is not working. The consumerGroupIdFilter query param in the url is not being mapped to the consumerGroupIdFilter variable of the ConsumerQueryParams object, whereas groupId and artifactId gets mapped.
I am not sure why this is happening. As far as I know, the ConsumerQueryParams class has the correct code. All that I did was to change the variable names and updated the getters and setters in the Builder class.
Can anyone help me here.


Solution

  • The problem is that the url has the new name and the annotation has the old one

    @QueryParam("groupIdFilter") 
    
    consumerGroupIdFilter