java 带有可选查询字符串的 Spring REST-ful uri

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15415167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:30:50  来源:igfitidea点击:

Spring REST-ful uri with optional querystring

javaspringrest

提问by user2170710

The normal uri which triggers the default controller to get all cars is just "/cars"

触发默认控制器获取所有汽车的普通 uri 只是“/cars”

I want to be able to search for cars aswell with an uri, for example: "/cars?model=xyz" which would return a list of matching cars. All the request parameters should be optional.

我希望能够使用 uri 搜索汽车,例如:“/cars?model=xyz”,它将返回匹配汽车的列表。所有请求参数都应该是可选的。

The problem is that even with the querystring the default controller triggers anyway and I always get "all cars: ..."

问题是,即使使用查询字符串,默认控制器也会触发,我总是得到“所有汽车:......”

Is there a way to do this with Spring without a separate search uri (like "/cars/search?..")?

有没有办法在没有单独搜索 uri(如“/cars/search?..”)的情况下用 Spring 做到这一点?

code:

代码:

@Controller
@RequestMapping("/cars")
public class CarController {
@Autowired
private CarDao carDao;

@RequestMapping(method = RequestMethod.GET, value = "?")
public final @ResponseBody String find(
        @RequestParam(value = "reg", required = false) String reg,
        @RequestParam(value = "model", required = false) String model
        )
{
    Car searchForCar = new Car();
    searchForCar.setModel(model);
    searchForCar.setReg(reg);
    return "found: " + carDao.findCar(searchForCar).toString();
}

@RequestMapping(method = RequestMethod.GET)
public final @ResponseBody String getAll() {
    return "all cars: " + carDao.getAllCars().toString();
} 
}

采纳答案by DwB

Try a variation of this:

试试这个的变体:

    @Controller
    @RequestMapping("/cars")
    public clas CarController
    {
        @RequestMapping(method = RequestMethod.get)
        public final @ResponseBody String carsHandler(
            final WebRequest webRequest)
        {
            String parameter = webRequest.getParameter("blammy");

            if (parameter == null)
            {
                return getAll();
            }
            else
            {
                return findCar(webRequest);
            }
        }
    }

回答by Sotirios Delimanolis

You can use

您可以使用

@RequestMapping(method = RequestMethod.GET, params = {/* string array of params required */})
public final @ResponseBody String find(@RequestParam(value = "reg") String reg, @RequestParam(value = "model") String model)
    // logic
}

ie, the @RequestMappingannotation has a property called params. If allof the parameters that you specify are contained in your request (and all other RequestMappingrequirements match), then that method will be called.

即,@RequestMapping注释有一个名为 的属性params。如果您指定的所有参数都包含在您的请求中(并且所有其他RequestMapping要求都匹配),则将调用该方法。