如何使用 @RequestParam 捕获多个 URL 参数?

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

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(&quot;/restaurant&quot;)
public List&lt;Restaurant&gt; list(@RequestParam(required = false) String nameRestaurant, @RequestParam(required = false)String nameProduct){
    if (nameRestaurant== null &amp;&amp; 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语句检查nameRestaurantnameProduct是否为null,这意味着在elseelse if中,nameRestaurantnameProduct可能为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(&quot;/restaurant&quot;)
public List&lt;restaurant&gt; list(@RequestParam(required = false) String nameRestaurant,
                             @RequestParam(required = false) String nameProduct) {
   if (nameRestaurant != null &amp;&amp; !nameRestaurant.isEmpty()) {
      return restaurantRepository.findByNameRestaurant(nameRestaurant);
   }
   if (nameProduct != null &amp;&amp; !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(&quot;/restaurant&quot;)
/restaurant

// Get restaurant by name
@GET
@Path(&quot;/restaurant&quot;)
@PathParam(&quot;name&quot;)String name
/restaurant/{name}

// Get product by restaurant and product name
@GET
@Path(&quot;/restaurant&quot;)
@PathParam(&quot;name&quot;)String name, @PathParam(&quot;product&quot;)String product
/restaurant/{name}/{product}

huangapple
  • 本文由 发表于 2020年8月17日 20:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63451015.html
匿名

发表评论

匿名网友

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

确定