多个查询值绑定在POJO中

huangapple go评论69阅读模式
英文:

Multiple query values binding in POJO

问题

我有一个简单的控制器和REST API,如下所示,我想将 'name'、'price' 和 'description' 查询值映射到 ProductSearchCriteria 中:

@Controller("/api/v1/product")
public class ProductController {
    private static final Logger LOG = LoggerFactory.getLogger(ProductController.class);

    @Operation(summary = "Find all products")
    @ApiResponses(value = {@ApiResponse(content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ProductViewModel.class))}})
    @Get(uri="/")
    List<ProductViewModel> find(@Valid ProductSearchCriteria searchCriteria) {
        LOG.info("Finding all the products");
        //return productProducer.find(searchCriteria);
        return new ArrayList<>();
    }
}

产品搜索条件

public class ProductSearchCriteria {
    @JsonProperty("name")
    Optional<String> name;

    @JsonProperty("price")
    Optional<Float> price;

    @JsonProperty("description")
    Optional<String> description;

    public Optional<String> getName() {
        return name;
    }

    public void setName(Optional<String> name) {
        this.name = name;
    }

    public Optional<Float> getPrice() {
        return price;
    }

    public void setPrice(Optional<Float> price) {
        this price = price;
    }

    public Optional<String> getDescription() {
        return description;
    }

    public void setDescription(Optional<String> description) {
        this.description = description;
    }
}

错误

错误:400 Bad Request
{
  "message": "Required argument [ProductSearchCriteria searchCriteria] not specified",
  "path": "/searchCriteria",
  "_links": {
    "self": {
      "href": "/api/v1/product?name=string&price=0&description=string",
      "templated": false
    }
  }
}
英文:

I have a simple controller and rest API as below in which I want to map 'name', 'price' and 'description' querie values into ProductSearchCriteria :

    @Controller(&quot;/api/v1/product&quot;)
    public class ProductController {
        private static final Logger LOG = LoggerFactory.getLogger(ProductController.class);
    
        @Operation(summary = &quot;Find all products&quot;)
        @ApiResponses(value = {@ApiResponse(content = {@Content(mediaType = &quot;application/json&quot;, schema = @Schema(implementation = ProductViewModel.class))})})
        @Get(uri=&quot;/&quot;)
        List&lt;ProductViewModel&gt; find(@Valid ProductSearchCriteria searchCriteria) {
            LOG.info(&quot;Finding all the products&quot;);
            //return productProducer.find(searchCriteria);
            return new ArrayList&lt;&gt;();
        }
    }

product search criteria

public class ProductSearchCriteria {
    @JsonProperty(&quot;name&quot;)
    Optional&lt;String&gt; name;

    @JsonProperty(&quot;price&quot;)
    Optional&lt;Float&gt; price;

    @JsonProperty(&quot;description&quot;)
    Optional&lt;String&gt; description;

    public Optional&lt;String&gt; getName() {
        return name;
    }

    public void setName(Optional&lt;String&gt; name) {
        this.name = name;
    }

    public Optional&lt;Float&gt; getPrice() {
        return price;
    }

    public void setPrice(Optional&lt;Float&gt; price) {
        this.price = price;
    }

    public Optional&lt;String&gt; getDescription() {
        return description;
    }

    public void setDescription(Optional&lt;String&gt; description) {
        this.description = description;
    }
}

Error

Error: 400 Bad Request
    {
      &quot;message&quot;: &quot;Required argument [ProductSearchCriteria searchCriteria] not specified&quot;,
      &quot;path&quot;: &quot;/searchCriteria&quot;,
      &quot;_links&quot;: {
        &quot;self&quot;: {
          &quot;href&quot;: &quot;/api/v1/product?name=string&amp;price=0&amp;description=string&quot;,
          &quot;templated&quot;: false
        }
      }
    }

答案1

得分: 2

您在`@Get`注解上漏掉了一些内容

```java
    @Operation(summary = "查找所有产品")
    @ApiResponses(value = {@ApiResponse(content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ProductViewModel.class))})})
    @Get(uri="/{?searchCriteria*}")
    List<ProductViewModel> find(@Valid ProductSearchCriteria searchCriteria) {
        LOG.info("查找所有产品");
        //return productProducer.find(searchCriteria);
        return new ArrayList<>();
    }

您还需要在ProductSearchCriteria的POJO上添加@Introspected注解。

由于您在使用@Valid,我建议在ProductSearchCriteria内部使用验证注解,而不是Optional


<details>
<summary>英文:</summary>

You missed something on the `@Get` annotation

```java
    @Operation(summary = &quot;Find all products&quot;)
    @ApiResponses(value = {@ApiResponse(content = {@Content(mediaType = &quot;application/json&quot;, schema = @Schema(implementation = ProductViewModel.class))})})
    @Get(uri=&quot;/{?searchCriteria*}&quot;)
    List&lt;ProductViewModel&gt; find(@Valid ProductSearchCriteria searchCriteria) {
        LOG.info(&quot;Finding all the products&quot;);
        //return productProducer.find(searchCriteria);
        return new ArrayList&lt;&gt;();
    }

You also miss an @Introspected on your ProductSearchCriteria POJO.

Since you are using @ValidI would go with validation annotation inside ProductSearchCriteria in place of Optional

huangapple
  • 本文由 发表于 2020年10月6日 20:20:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64225688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定