Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide default values for array parameters in spring MVC url mapping?

@RequestMapping(value = "/getUserScoreCardDetails", method = RequestMethod.GET)
public @ResponseBody List<ScoreDetails> getUserScoreCardDetails(
@RequestParam(value = "playerIds", required = false) int[] playerIds) { 

}

I need to provide default values [1,2,3] for playerIds if playerIds is not available in request?

like image 802
Dhakchianandan Avatar asked Feb 19 '17 15:02

Dhakchianandan


People also ask

What are the uses of @RequestMapping and @restcontroller annotations in Spring boot?

Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It's used to mark a class as a web request handler.

What is @RequestMapping annotation used for?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

What is the use of @RequestMapping in Spring?

One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to handler methods of controllers.


1 Answers

You can set comma separated values inside defaultValue property in @RequestParam

@RequestMapping(value = "/getUserScoreCardDetails", method = RequestMethod.GET)
public @ResponseBody List<ScoreDetails> getUserScoreCardDetails(
@RequestParam(value = "playerIds", required = false, defaultValue="1,2,3") int[] playerIds) { 

}
like image 128
Monzurul Shimul Avatar answered Oct 05 '22 17:10

Monzurul Shimul