我想验证表单输入。特别是name
参数,其最大长度为255个字符。但我得到了这个例外
UnexpectedTypeException: HV000030: No validator could be found for constraint org.hibernate.validator.constraints.Length validating type char[]. Check configuration for ...
用于以下映射:
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@Validated
@RestController
@RequestMapping("/profile")
public class ProfileController {
@PostMapping(value = "/savename")
public Response saveName(
@Length(max = 255)
@RequestParam(value = "name", required = true) char[] name
) {
return ...
}
}
怎么了?
答案 0 :(得分:0)
@Length
仅适用于String
。对于字符数组,请改用@Size
。这是正确的映射:
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.Size;
@Validated
@RestController
@RequestMapping("/profile")
public class ProfileController {
@PostMapping(value = "/savename")
public Response saveName(
@Size(max = 255)
@RequestParam(value = "name", required = true) char[] name
) {
return ...
}
}