Required String parameter is not present Spring MVC
Asked Answered
O

5

10

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.

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?

Officialdom answered 20/9, 2016 at 8:6 Comment(1)
@RequestParam(value = "views", required = false) or use defaultValueCathead
D
28

Add required=false attribute to @RequestParam..Change to

@RequestParam(required=false,name="views") String view,..
Distributary answered 20/9, 2016 at 8:9 Comment(2)
Oh my god.! I do not have words to say thanks. Kept browsing through everywhere for this. Thanks a ton. Wish I could give million up-votes.Lexicostatistics
@AjayKumar I feel you bro!Janycejanyte
W
4

Add required=false to the @RequestParam notation for both. Or you could decide to explicitly use one, set it to required=false and set the other as the defaultValue.

See the documentation for further information.

Wareroom answered 20/9, 2016 at 8:9 Comment(0)
B
2

I was using Postman for API hit, Adding required=false and defalutValue might be helpful, but required=false results in NULL Exception. Then I have Removed the Headers (can Authorization header) in Postman and its works.

Bluebell answered 21/3, 2019 at 3:14 Comment(0)
S
0

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

Strainer answered 19/3, 2018 at 5:10 Comment(0)
H
-1

Instead of:

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

Do this:

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

Add brackets to the end of the param, anything else as normal.

Hagans answered 20/7, 2021 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.