Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 应为带“的请求参数”;“异或”;验证_Spring_Spring Mvc - Fatal编程技术网

Spring 应为带“的请求参数”;“异或”;验证

Spring 应为带“的请求参数”;“异或”;验证,spring,spring-mvc,Spring,Spring Mvc,我在SpringMVC控制器中有一个简单的请求调用 @ResponseBody @RequestMapping(value = "/url", method = RequestMethod.GET) public SomeDTO getSth(@RequestParam("paramA") Integer paramA, @RequestParam("paramB") Integer paramB) { // ... } 我想让paramA或paramB都有一个正常的http响应,如果

我在SpringMVC控制器中有一个简单的请求调用

@ResponseBody
@RequestMapping(value = "/url", method = RequestMethod.GET)
public SomeDTO getSth(@RequestParam("paramA") Integer paramA, @RequestParam("paramB") Integer paramB) {
    // ...
}
我想让paramA或paramB都有一个正常的http响应,如果我不同时提供这两个参数的话


我知道有一个必需的参数可用,但我看不到连接这两个参数的方法。有什么想法吗?

我想不出一个很好的解决方案,但直截了当的解决方案似乎很正常,不是吗

@ResponseBody
@RequestMapping(value = "/url", method = RequestMethod.GET)
public String getSth(@RequestParam(value = "paramA", required = false) Integer paramA,
        @RequestParam(value = "paramB", required = false) Integer paramB) {
    if (paramA == null ^ paramB == null) {
        return "body";
    } else {
        throw new BadRequestException();
    }
}

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public static class BadRequestException extends RuntimeException {
    private static final long serialVersionUID = 1L;
}