Java 所需的字符串参数不存在 Spring MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39588801/
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-08-11 21:16:45  来源:igfitidea点击:

Required String parameter is not present Spring MVC

javaspringspring-mvchttp-request-parameters

提问by kosas

I want to access two different pages in my browser using:

我想使用以下方法在浏览器中访问两个不同的页面:

http://localhost:8080/name?views

and

http://localhost:8080/name?uviews

But I'm getting error Required String parameter 'uviews' is not present when I use first url and I get Required String parameter 'views' is not present when I use second one.

但是当我使用第一个 url 时,我收到错误 Required String parameter 'uviews' is not present,当我使用第二个 url 时,我得到 Required String parameter 'views' is not present。

here is my Response body

这是我的响应正文

@ResponseBody     
public Object getViewInJson(@RequestParam("views") String views ,@RequestParam("uviews") String uviews) throws IOException{

 loader = new AnalyticsLoader();



    return loader.AnalyticsLoader(views,uviews);
}

How can access both views and uviews?

如何访问视图和uviews?

采纳答案by Prasanna Kumar H A

Add required=falseattribute to @RequestParam..Change to

required=false属性添加到@RequestParam..Change to

@RequestParam(required=false,name="views") String view,..

回答by ChiefTwoPencils

Add required=falseto the @RequestParamnotation for both. Or you could decide to explicitly use one, set it to required=falseand set the other as the defaultValue.

添加required=false@RequestParam符号两种。或者您可以决定明确使用一个,将其required=false设置为并将另一个设置为defaultValue.

See the documentationfor further information.

有关更多信息,请参阅文档

回答by Lyn

please check your spring version, there is a bug in 3.2.3. https://jira.spring.io/browse/SPR-10592

请检查您的 spring 版本,3.2.3 中有一个错误。 https://jira.spring.io/browse/SPR-10592

回答by Patel Romil

I was using Postmanfor API hit, Adding required=falseand defalutValuemight be helpful, but required=falseresults in NULL Exception. Then I have Removed the Headers (can Authorization header)in Postman and its works.

我正在使用PostmanAPI 命中,添加required=false并且defalutValue可能会有所帮助,但required=false结果是NULL Exception. 然后我Removed the Headers (can Authorization header)在邮递员及其作品中。