Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java get url中RequestParam的允许值_Java_Spring Boot_Mybatis_Spring Mybatis - Fatal编程技术网

Java get url中RequestParam的允许值

Java get url中RequestParam的允许值,java,spring-boot,mybatis,spring-mybatis,Java,Spring Boot,Mybatis,Spring Mybatis,是否可以为@RequestParam(value=“id”)字符串id) ? 理想情况是:我给出一些列表允许值,它会自动验证它。(此列表将从数据库中加载) 这是可能的,但不是某种神奇的方法,而是通过简单的验证检查 例如: @RequestMapping(value = "yourRestEndPoint", method = RequestMethod.GET) public anyReturnType methodName(@RequestParam(value = "id") String

是否可以为
@RequestParam(value=“id”)字符串id)

?


理想情况是:我给出一些
列表允许值
,它会自动验证它。(此列表将从数据库中加载)

这是可能的,但不是某种神奇的方法,而是通过简单的验证检查

例如:

@RequestMapping(value = "yourRestEndPoint", method = RequestMethod.GET)
public anyReturnType methodName(@RequestParam(value = "id") String id){

     List<String> allowedValues = getAllowedValuesFromDB();

     if(allowedValues.contains(id)){//check if incoming ID belongs to Allowed Values
     //Do your action....
     }
}

private List<String> getAllowedValuesFromDB(){

    List<String> allowedValues = new ArrayList<>();
    //fetch list from DB
    //set fetched values to above List

    return allowedValues;
}
@RequestMapping(value=“yourRestEndPoint”,method=RequestMethod.GET)
public anyReturnType方法名(@RequestParam(value=“id”)字符串id){
List allowedValues=getAllowedValuesFromDB();
if(allowedValues.contains(id)){//检查传入id是否属于允许的值
//做你的行动。。。。
}
}
私有列表getAllowedValuesFromDB(){
List allowedValues=new ArrayList();
//从数据库获取列表
//将获取的值设置为上述列表
返回允许的值;
}
第二种方式

如果您想像我们做Bean验证一样,使用@Valid,这不仅是Bean的一个参数,而且还需要配置验证器,那么请签出并回答


希望这有帮助。

使用枚举而不是字符串,并定义枚举值

@RequestMapping(value = "yourRestEndPoint", method = RequestMethod.GET)
public anyReturnType methodName(@RequestParam(defaultValue = "ONE") IdValue id) {
 ....
}
并定义一个单独的类,例如

public enum IdValue {
  ONE, TWO, THREE
}