java Spring MVC - 如何在缺少参数时返回 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10372632/
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
Spring MVC - how to return 404 on missing parameters
提问by solyd
I want to trigger 404 page whenever I wasn't passed all of the parameters. Lets say I have the following URI:
每当我没有传递所有参数时,我都想触发 404 页面。假设我有以下 URI:
/myapp/op?param1=1¶m2=2@param3=3
In case on of the parameters wasn;t invoked I want to return 404 page. I tried doing:
如果没有调用参数,我想返回 404 页面。我试着做:
@ResponseStatus(HttpStatus.NOT_FOUND)
@RequestMapping(value = "op", params = { "!param1" })
public void missingArg() {
}
but then I get an exception telling me there is ambiguity between methods that handle missing second and third parameter.
但是后来我收到一个异常,告诉我处理丢失的第二个和第三个参数的方法之间存在歧义。
How can I accomplish this, then?
那我怎样才能做到这一点呢?
回答by Tim Pote
If you're using Spring 3.1 you can define an exception class like so:
如果您使用的是 Spring 3.1,您可以像这样定义一个异常类:
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public final class ResourceNotFoundException extends RuntimeException {
// class definition
}
Now whenever you throw that exception, Spring will return the http status defined in your @ResponseStatus
annotation. For example:
现在,无论何时抛出该异常,Spring 都会返回@ResponseStatus
注释中定义的 http 状态。例如:
@RequestMapping(value = "/op")
public void methodWithRequestParams(@RequestParam(value = "param1", required = false) String param1,
@RequestParam(value = "param2", required = false) String param2) {
if (param1 == null || param2 == null) {
throw new ResourceNotFoundException();
}
}
will return a 404
whenever param1
or param2
is null.
将在404
任何时候param1
或param2
为空时返回。
回答by matsev
You do not have to implement the missingArg()
function. If there is no matching method for the incoming request, then Spring's HandlerExceptionResolverwill handle it and return a response with an appropriate status code.
您不必实现该missingArg()
功能。如果传入的请求没有匹配的方法,那么 Spring 的HandlerExceptionResolver将处理它并返回一个带有适当状态码的响应。
Spring will automatically convert the request parameters into method parameters if you use the @RequestParamannotation:
如果使用@RequestParam注解,Spring 会自动将请求参数转换为方法参数:
@RequestMapping(value = "/op")
public void methodWithRequestParams(@RequestParam("param1") String param1,
@RequestParam("param2") String param2,
@RequestParam("param3") String param3) {
// do something with params
}
By convention, the methodWithRequestParams()
method will not be called if not all params are part of the request (unless the required
attribute of the @RequestParam
is set to false
).
按照惯例,methodWithRequestParams()
如果不是所有参数都是请求的一部分,则不会调用该方法(除非将 的required
属性@RequestParam
设置为false
)。
Also note that the parameters does not have to be Strings.
另请注意,参数不必是字符串。
回答by fivedogit
Echoing what matsev said in the comments of another answer, you should not be using @ResponseStatus(HttpStatus.NOT_FOUND)
in this case, but rather @ResponseStatus(HttpStatus.BAD_REQUEST)
.
回应 matsev 在另一个答案的评论中所说的话,@ResponseStatus(HttpStatus.NOT_FOUND)
在这种情况下您不应该使用@ResponseStatus(HttpStatus.BAD_REQUEST)
.
@ResponseStatus(HttpStatus.NOT_FOUND)
should be used when the request was formed properly, but the resource isn't there.
@ResponseStatus(HttpStatus.NOT_FOUND)
应该在请求正确形成但资源不存在时使用。