英文:
How to capture multiple url parameters using @RequestParam?
问题
我有这个方法:
@GetMapping("/restaurant")
public List<Restaurant> list(@RequestParam(required = false) String nameRestaurant, @RequestParam(required = false) String nameProduct){
if (nameRestaurant == null && nameProduct == null ) {
return restaurantRepository.findAll();
}
else if (!nameProduct.isEmpty()){
return restaurantRepository.findByNameProduct(nameProduct);
}
else {
return restaurantRepository.findByNameRestaurant(nameRestaurant);
}
}
只有 'IF' 和 'ELSE IF' 能正常工作并返回 JSON,而 'ELSE' 总是返回 500(NullPointerException)。如果我将 'ELSE IF' 和 'ELSE' 位置对调,原本在 'ELSE IF' 的部分将会工作,而 'ELSE' 部分则不会,是否有原因呢?
英文:
I have this method:
@GetMapping("/restaurant")
public List<Restaurant> list(@RequestParam(required = false) String nameRestaurant, @RequestParam(required = false)String nameProduct){
if (nameRestaurant== null && nameProduct== null ) {
return restaurantRepository.findAll();
}
else if (!nameProduct.isEmpty()){
return restaurantRepository.findByNameProduct(nameProduct);
}
else {
return restaurantRepository.findByNameRestaurant(nameRestaurant);
}
}
Only 'IF' and 'ELSE IF' work and return JSON, 'ELSE' always returns 500(NullPointerException). If I invert 'ELSE IF' with 'ELSE', what will be in 'ELSE IF' will work and 'ELSE' will not, is there any why?
答案1
得分: 2
我认为你在验证部分缺少了一个空值检查。第一个if语句检查nameRestaurant
和nameProduct
是否为null
,这意味着在else
或else if
中,nameRestaurant
或nameProduct
可能为null
(可能导致NullPointerException
)。
可能将你的代码更改如下会起作用:
@GetMapping("/restaurant")
public List<restaurant> list(@RequestParam(required = false) String nameRestaurant,
@RequestParam(required = false) String nameProduct) {
if (nameRestaurant != null && !nameRestaurant.isEmpty()) {
return restaurantRepository.findByNameRestaurant(nameRestaurant);
}
if (nameProduct != null && !nameProduct.isEmpty()) {
return restaurantRepository.findByNameProduct(nameProduct);
}
return restaurantRepository.findAll();
}
英文:
I think you are missing a null checker in your validation. The first if checks if nameRestaurant
AND nameProduct
are null
, meaning that during the else
/ else if
nameRestaurant
OR nameProduct
might be null
(possibly leading to NullPointerException
).
Probably changing your code to the following would work:
@GetMapping("/restaurant")
public List<restaurant> list(@RequestParam(required = false) String nameRestaurant,
@RequestParam(required = false) String nameProduct) {
if (nameRestaurant != null && !nameRestaurant.isEmpty()) {
return restaurantRepository.findByNameRestaurant(nameRestaurant);
}
if (nameProduct != null && !nameProduct.isEmpty()) {
return restaurantRepository.findByNameProduct(nameProduct);
}
return restaurantRepository.findAll();
}
答案2
得分: 0
use urls instead:
// 获取所有餐厅
@GetMapping("/restaurant")
/restaurant
// 根据名称获取餐厅
@GET
@Path("/restaurant")
@PathParam("name")String name
/restaurant/{name}
// 根据餐厅和产品名称获取产品
@GET
@Path("/restaurant")
@PathParam("name")String name, @PathParam("product")String product
/restaurant/{name}/{product}
英文:
use urls instead:
// Get all restaurants
@GetMapping("/restaurant")
/restaurant
// Get restaurant by name
@GET
@Path("/restaurant")
@PathParam("name")String name
/restaurant/{name}
// Get product by restaurant and product name
@GET
@Path("/restaurant")
@PathParam("name")String name, @PathParam("product")String product
/restaurant/{name}/{product}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论