gpt4 book ai didi

java - 查询参数未映射

转载 作者:行者123 更新时间:2023-12-02 13:25:35 25 4
gpt4 key购买 nike

我有一个 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 上的数据。现在我有一个要求,需要将 ConsumerQueryParams 类中的 groupIdFilterartifactIdFilterversionFilter 查询参数重命名为分别为 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);
}
}
}

现在我尝试使用以下网址访问相同的端点:

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

但这不起作用。网址中的 consumerGroupIdFilter 查询参数未映射到 ConsumerQueryParams 对象的 consumerGroupIdFilter 变量,而 groupId并且 artifactId 被映射。
我不知道为什么会发生这种情况。据我所知,ConsumerQueryParams 类具有正确的代码。我所做的只是更改变量名称并更新 Builder 类中的 getter 和 setter。
任何人都可以帮助我吗?

最佳答案

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

@QueryParam("groupIdFilter") 

consumerGroupIdFilter

关于java - 查询参数未映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43399101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com