我有一个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
模式设置变量。
我使用以下网址访问上面指定的端点。
http://localhost:8080/rest/api/consumers/paged?groupId=org.slf4j&artifactId=slf4j-api&groupIdFilter=sdlc
一切正常。我能够成功获取Postman
上的数据。
现在,我需要将groupIdFilter
类中的artifactIdFilter
,versionFilter
,ConsumerQueryParams
查询参数重命名为consumerGroupIdFilter
,consumerArtifactIdFilter
和分别为consumerVersionFilter
更改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
但这不起作用。网址中的consumerGroupIdFilter
查询参数未映射到consumerGroupIdFilter
对象的ConsumerQueryParams
变量,而groupId
和artifactId
被映射。
我不确定为什么会这样。据我所知,ConsumerQueryParams
类具有正确的代码。我所做的就是更改变量名称并更新Builder
类中的getter和setter
任何人都可以帮助我。
答案 0 :(得分:1)
问题是url具有新名称,而注释具有旧名称
@QueryParam("groupIdFilter")
consumerGroupIdFilter