javax.servlet.ServletException:HV000030:找不到类型为:char []

时间:2017-11-13 10:39:29

标签: java validation spring-mvc maxlength

我想验证表单输入。特别是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 ...
    }

}

怎么了?

1 个答案:

答案 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 ...
    }

}