如何在spring MVC url mapping中为数组参数提供默认值?

时间:2017-02-19 15:45:00

标签: spring spring-mvc

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

}

如果请求中没有playerIds,我需要为playerIds提供默认值[1,2,3]吗?

2 个答案:

答案 0 :(得分:2)

您可以在@RequestParam

中的defaultValue属性中设置逗号分隔值
library(ggplot2)
library(dplyr)
library(scales)

df %>% 
  gather('key','value',-ID, -Group) %>% 
  group_by(Group, key) %>% 
  summarise(mean = mean(value),
            sem = sd(value) / sqrt(n()),
            lower = (mean - sem),
            upper = (mean + sem))-> newdf

#Draw bar plot with ggplot2
plot <- ggplot(data=newdf, aes(x=Group, y=mean, fill=key)) + 
  geom_bar(stat="identity",
           position="stack",
           width = 0.4) +
  geom_errorbar(data = filter(newdf, key == 'Unlabeled'), aes(ymax=upper,  ymin=lower), stat = 'identity', position = 'identity', width=0.15) +
  xlab(" ") + 
  ylab("Percentage (%)") +
  scale_y_continuous(labels = function(bs) {paste0(bs, '%')}) +
  theme_classic(base_size = 16, base_family = "Helvetica") + 
  theme(axis.text.y=element_text(size=16, face="bold"), 
        axis.title.y=element_text(size=16, face="bold"),
        axis.text.x=element_text(angle=45,hjust=1,vjust=1, size=16, face="bold"),
        legend.position="right")

答案 1 :(得分:0)

在您的方法中,只需检查playerIds是否为null,如果是null,则指定默认值

@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};
     }
}