英文:
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("/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<>();
}
}
product search criteria
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;
}
}
Error
Error: 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
}
}
}
答案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 = "Find all products")
@ApiResponses(value = {@ApiResponse(content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ProductViewModel.class))})})
@Get(uri="/{?searchCriteria*}")
List<ProductViewModel> find(@Valid ProductSearchCriteria searchCriteria) {
LOG.info("Finding all the products");
//return productProducer.find(searchCriteria);
return new ArrayList<>();
}
You also miss an @Introspected
on your ProductSearchCriteria
POJO.
Since you are using @Valid
I would go with validation annotation inside ProductSearchCriteria
in place of Optional
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论