Java 在Spring中将类级验证添加到@Pathvariable

Java 在Spring中将类级验证添加到@Pathvariable,java,spring,rest,validation,spring-mvc,Java,Spring,Rest,Validation,Spring Mvc,在发布这个问题之前,我做了很多研究,尝试了很多解决方案 这是我陷入的棘手局面 我有一个Spring控制器,它有多个请求映射,它们都有@PathVariables 以下是控制器的外观: @Controller @EnableWebMvc public class ChildController extends ParentController<InterfaceController> implements InterfaceController{ @Override

在发布这个问题之前,我做了很多研究,尝试了很多解决方案

这是我陷入的棘手局面

我有一个Spring控制器,它有多个请求映射,它们都有@PathVariables

以下是控制器的外观:

@Controller
@EnableWebMvc
public class ChildController extends ParentController<InterfaceController> implements InterfaceController{

    @Override
    @RequestMapping(value = "/map/{name}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
    @ResponseStatus( HttpStatus.OK)
    @ResponseBody   
    public List<Friends> getAllFriendsByName(
        @PathVariable("name") String name,
        @RequestParam(value="pageSize", required=false) String pageSize,
        @RequestParam(value="pageNumber", required=false) String pageNumber,            
        HttpServletRequest request) throws BasicException {

    //Some logic over here;

    return results;
    }

@Override
    @RequestMapping(value = "/map/{id}", produces = "application/json; charset=UTF-8", method = RequestMethod.GET)
    @ResponseStatus( HttpStatus.OK)
    @ResponseBody   
    public List<Friends> getAllFriendsById(
        @PathVariable("id") String id,
        @RequestParam(value="pageSize", required=false) String pageSize,
        @RequestParam(value="pageNumber", required=false) String pageNumber,            
        HttpServletRequest request) throws BasicException {

    //Some logic over here;

    return results;
    }

}
@控制器
@EnableWebMvc
公共类ChildController扩展ParentController实现InterfaceController{
@凌驾
@RequestMapping(value=“/map/{name}”,products=“application/json;charset=UTF-8”,method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@应答器
公共列表getAllFriendsByName(
@路径变量(“名称”)字符串名称,
@RequestParam(value=“pageSize”,required=false)字符串pageSize,
@RequestParam(value=“pageNumber”,required=false)字符串pageNumber,
HttpServletRequest请求)引发基本异常{
//这里有一些逻辑;
返回结果;
}
@凌驾
@RequestMapping(value=“/map/{id}”,products=“application/json;charset=UTF-8”,method=RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@应答器
公共列表getAllFriendsById(
@PathVariable(“id”)字符串id,
@RequestParam(value=“pageSize”,required=false)字符串pageSize,
@RequestParam(value=“pageNumber”,required=false)字符串pageNumber,
HttpServletRequest请求)引发基本异常{
//这里有一些逻辑;
返回结果;
}
}
现在我最近了解到,您无法在Spring中验证Path变量,与我尝试过的这些示例相反,结果证明它不起作用 (示例:)

因此,我考虑将自定义JSR303约束验证添加到我的路径变量中,并创建类似于我自己的东西

 import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import java.lang.annotation.Documented;
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
 import javax.validation.Constraint;
 import javax.validation.Payload;

 @Target({ TYPE})
 @Retention(RUNTIME)
 @Constraint(validatedBy = PathVariableValidator.class)
 @Documented
 public @interface RequestValidationNeeded {

String id();

String name();

Class<?>[] constraints() default {};

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

String message() default "totally wrong, dude!";

 }
导入静态java.lang.annotation.ElementType.TYPE;
导入静态java.lang.annotation.RetentionPolicy.RUNTIME;
导入java.lang.annotation.Documented;
导入java.lang.annotation.Retention;
导入java.lang.annotation.Target;
导入javax.validation.Constraint;
导入javax.validation.Payload;
@目标({TYPE})
@保留(运行时)
@约束(validatedBy=PathVariableValidator.class)
@记录
public@interface RequestValidationNeeded{
字符串id();
字符串名();
类[]约束()默认值{};
类[]组()默认值{};

ClassIn在Patrick()提供的解决方案中,他特别要求向控制器添加@Validated注释,并向path变量添加@Size注释,例如:
@PathVariable(“name”)@Size(min=1,max=10)字符串名
。然后添加他帖子中的剩余代码,看看是否有效。应该可以。@R4J我试过了。发布的代码是我的本地代码,而不是我试过的代码。我添加了@Validated并添加了@Size验证。