Spring boot 如何使用多个值将来自axios的GET请求传递给Spring
我在React to filter products中创建了一个过滤器组件,它有多个复选框可供选择(作为数组类别[]),但在后端(spring boot)中接收多个值时遇到了问题。我的想法是发送一个数组,放入一个WHERE IN(:category_id)中 当端点为:Spring boot 如何使用多个值将来自axios的GET请求传递给Spring,spring-boot,jpa,axios,Spring Boot,Jpa,Axios,我在React to filter products中创建了一个过滤器组件,它有多个复选框可供选择(作为数组类别[]),但在后端(spring boot)中接收多个值时遇到了问题。我的想法是发送一个数组,放入一个WHERE IN(:category_id)中 当端点为: http://localhost:8080/api/v1/products/?category_id=1 它起作用了 对于我尝试的多个值(通过邮递员): 第一个仅显示1个结果,其他结果不被接受(http错误400)。因此,格式
http://localhost:8080/api/v1/products/?category_id=1
它起作用了
对于我尝试的多个值(通过邮递员):
第一个仅显示1个结果,其他结果不被接受(http错误400)。因此,格式中一定有错误
在控制器上,我创建了一个过滤器端点:
@RequestMapping(value="/products/")
@ResponseBody
public ResponseEntity<Object> filterProducts(
@RequestParam Optional<Long> price,
@RequestParam Optional<Long> category_id,
@RequestParam Optional<String> taste,
@RequestParam Optional<String> name,
@RequestParam Optional<Long> manufacturer_id) {
List<Product> products = productRepository.findProductByFilterValues(price, category_id, taste, name, manufacturer_id);
return new ResponseEntity<>(products, HttpStatus.OK);
}
@RequestMapping(value=“/products/”)
@应答器
公共响应过滤器产品(
@RequestParam可选价格,
@RequestParam可选类别\u id,
@RequestParam可选味道,
@RequestParam可选名称,
@RequestParam(可选制造商id){
列出产品=productRepository.FindProductByFilterValue(价格、类别id、口味、名称、制造商id);
返回新响应(产品,HttpStatus.OK);
}
在存储库中,我做了一个自定义查询:
@Query( "SELECT p from Product p WHERE (:name is null or p.name LIKE %:name%) " +
"AND (p.price = :price or :price is null) " +
"AND (p.categoryId IN (:category_id) OR :category_id is null) " +
"AND (p.manufacturer_id = :manufacturer_id OR :manufacturer_id is null) " +
"AND (p.taste = :taste OR :taste is null) "
)
List<Product> findProductByFilterValues(Optional<Long> price, Optional<Long> category_id, Optional<String> taste, Optional<String> name, Optional<Long> manufacturer_id);
@Query(“从产品p中选择p,其中(:name为null或p.name如%:name%)”+
“和(p.price=:price或:price为空)”+
“和(:category\u id)或:category\u id中的p.categoryId为空)”+
“和(p.manufacturer\u id=:manufacturer\u id或:manufacturer\u id为空)”+
“和(p.taste=:taste或:taste为空)”
)
列出FindProductByFilterValue(可选价格、可选类别标识、可选口味、可选名称、可选制造商标识);
哪里出了问题,我应该如何格式化
@Query( "SELECT p from Product p WHERE (:name is null or p.name LIKE %:name%) " +
"AND (p.price = :price or :price is null) " +
"AND (p.categoryId IN (:category_id) OR :category_id is null) " +
"AND (p.manufacturer_id = :manufacturer_id OR :manufacturer_id is null) " +
"AND (p.taste = :taste OR :taste is null) "
)
List<Product> findProductByFilterValues(Optional<Long> price, Optional<Long> category_id, Optional<String> taste, Optional<String> name, Optional<Long> manufacturer_id);