[英]Spring @RequestParam with a value/name of “status”
I am running a Spring boot MVC application, in my controller I have a @RequestParamter with a value of "status". 我正在运行一个Spring Boot MVC应用程序,在我的控制器中,我有一个@RequestParamter,其值为“ status”。 When I hit this method through a RequestMapping (eg http://localhost:8080/pathToMethod?status=someStatus&otherParam=aThing&status=anotherStatus )
当我通过RequestMapping(例如http:// localhost:8080 / pathToMethod?status = someStatus&otherParam = aThing&status = anotherStatus )使用此方法时
@RequestMapping(method = RequestMethod.GET, value ="pathToMethod")
public responseType methodName(
@RequestParam(value = "otherParam", required = false) List<String> param,
@RequestParam(value = "status", required = false) List<String> status,
HttpServletRequest request, HttpServletResponse response){
//code here
}
It fails with following error: 失败并显示以下错误:
Field error in object 'modelAndView' on field 'status': rejected value [Soemthing];
字段“状态”上的对象“ modelAndView”中的字段错误:拒绝的值[Soemthing]; codes [typeMismatch.modelAndView.status,typeMismatch.status,typeMismatch.org.springframework.http.HttpStatus,typeMismatch];
代码[typeMismatch.modelAndView.status,typeMismatch.status,typeMismatch.org.springframework.http.HttpStatus,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [modelAndView.status,status];
参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[modelAndView.status,status]; arguments [];
参数[]; default message [status]];
默认消息[状态]]; default message [Failed to convert property value of type [java.lang.String] to required type [org.springframework.http.HttpStatus] for property 'status';
默认消息[未能将类型[java.lang.String]的属性值转换为属性'status'所需的类型[org.springframework.http.HttpStatus]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [org.springframework.http.HttpStatus] for value 'Soemthing';
嵌套异常是org.springframework.core.convert.ConversionFailedException:无法从类型[java.lang.String]转换为类型[org.springframework.http.HttpStatus]的值“ Soemthing”; nested exception is java.lang.IllegalArgumentException: No enum constant org.springframework.http.HttpStatus.Soemthing]
嵌套异常是java.lang.IllegalArgumentException:没有枚举常量org.springframework.http.HttpStatus.Soemthing]
Is there any way to bypass Springs default handling of the "status" parameter so that it does not automatically try to convert it to a HttpResponse? 有什么方法可以绕过Springs对“ status”参数的默认处理,以便它不会自动尝试将其转换为HttpResponse?
This is happening because you are accepting request parameter as a List but passing only String. 发生这种情况是因为您接受请求参数作为列表,但仅传递了字符串。
Check the below possible reasons. 检查以下可能的原因。
1.Remove List<String>
and make it String
1.删除
List<String>
并使其成为String
2.Use any other name for request parameter instead of status
because status
is the default keyword for org.springframework.http.HttpStatus
2,使用其他任何名称作为请求参数而不是
status
因为status
是org.springframework.http.HttpStatus
的默认关键字
With Spring 4 and Java 8 it's possible to have optional path variables with the usage of Optional. 在Spring 4和Java 8中,可以通过使用Optional来具有可选的路径变量。
Code : 代码:
@RequestMapping(value = {"/path", "/path/{status}")
public String getResource(@PathVariable Optional<String> status) {
if (status.isPresent()) {
return service.processResource(status.get());
} else {
return service.processResource();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.