简体   繁体   中英

javax.servlet.ServletException: HV000030: No validator could be found for type: char[]

I want to validate a form input. Specifically a name parameter which has a maximum length of 255 characters. But I get this exception

UnexpectedTypeException: HV000030: No validator could be found for constraint org.hibernate.validator.constraints.Length validating type char[]. Check configuration for ...

for the following mapping:

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

}

What's wrong?

@Length works with String s only. For character arrays use @Size instead. Here's the correct mapping:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM