10
@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?

2 Answers 2

10

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) { 

}
Sign up to request clarification or add additional context in comments.

8 Comments

I tried this. But if playerIds is missing in request, playerIds is treated as null.
shouldn't be, this is well tested. please re.check
add System.out.println(Arrays.toString(playerIds)); inside the controller method and check the result.
I verified the result. Its coming as null. Might be because of old spring version. I am using spring.3.0.0
If anyone wants to default to an empty array, defaultValue="" does the trick. Works with lists too.
|
0

Inside your method, just check, if playerIds is null and if it is null then specify the default values there like this

@RequestMapping(value = "/getUserScoreCardDetails", method =
RequestMethod.GET)

public @ResponseBody List<ScoreDetails> getUserScoreCardDetails(
@RequestParam(value = "playerIds", required = false) int[] playerIds) {
     if(playerIds==null){
         playerIds = {1,2,3};
     }
}

4 Comments

I did the same thing. Thought there should be some better approach
i have read about this, and this is the elegant way to solve this problem... Just do it that way.. @Dhakchianandan
what you did now?@Dhakchianandan
i went with the above approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.