POJO 中的多个查询值绑定
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;
}
}
错误
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
}
}
}
您在 @Get
注释中遗漏了一些内容
@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<>();
}
您还错过了 ProductSearchCriteria
POJO 上的 @Introspected
。
因为你使用的是 @Valid
我会在 ProductSearchCriteria
中使用验证注释来代替 Optional
我有一个简单的控制器和 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;
}
}
错误
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
}
}
}
您在 @Get
注释中遗漏了一些内容
@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<>();
}
您还错过了 ProductSearchCriteria
POJO 上的 @Introspected
。
因为你使用的是 @Valid
我会在 ProductSearchCriteria
中使用验证注释来代替 Optional