找不到类型的验证器:java.lang.Integer

Posted

技术标签:

【中文标题】找不到类型的验证器:java.lang.Integer【英文标题】:No validator could be found for type: java.lang.Integer 【发布时间】:2016-04-16 00:01:47 【问题描述】:

尝试处理我的创建/编辑表单时出现以下错误:

HTTP 状态 500 - 请求处理失败;嵌套异常是 javax.validation.UnexpectedTypeException:HV000030:没有验证器可以 可以找到类型:java.lang.Integer。

我的控制者:

/**
 * This method will provide the medium to update an existing Game.
 *
 * @param id bla
 * @param model bla
 * @return bla
 */
@RequestMapping(value = "/edit?id=id", method = RequestMethod.GET)
public String initEditGame(
        @PathVariable int id,
        ModelMap model
) 
    Game game = gameService.findById(id);

    model.addAttribute("game", game);
    model.addAttribute("edit", true);
    return "host/games/createOrUpdate";


/**
 * This method will be called on form submission, handling POST request for
 * updating game in database. It also validates the user input
 *
 * @param game bla
 * @param id bla
 * @param result bla
 * @param model bla
 * @param locale blie
 * @return bloe
 */
@RequestMapping(value = "/edit?id=id", method = RequestMethod.POST)
public String processEditGame(
        @ModelAttribute("game") @Valid Game game,
        BindingResult result,
        ModelMap model,
        @PathVariable int id,
        Locale locale
) 

    if (result.hasErrors()) 
        return "host/games/createOrUpdate";
    

    gameService.update(game);

    model.addAttribute("success", "Game " + game.getName() + " updated successfully");
    return "host/games/createGameSucces";


我的模型课:

@Entity
@Table(name = "GAME")
public class Game implements Serializable 

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotEmpty
    @Column(name = "NAME", unique = true, nullable = false)
    private String name;

    @NotEmpty
    @Column(name = "DESCRIPTION", nullable = false)
    private String description;

    @NotNull
    @Length(min = 4, max = 4)
    @Column(name = "CODE", unique = true, nullable = false)
    private Integer code;

    @NotNull
    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name = "CREATED")
    @DateTimeFormat(pattern = "yyyy/MM/dd")
    private Date created;

    @NotNull
    @Column(name = "STATE", nullable = false)
    private String state = State.ACTIVE.getState();

    /**
     *
     */
    public Game() 
    

    /**
     *
     * @param name bla
     * @param description bleh
     */
    public Game(String name, String description) 
        this.name = name;
        this.description = description;
        this.code = generateRoomNumber();
    

有什么建议吗?

【问题讨论】:

【参考方案1】:

我认为问题在于:

@NotNull
@Length(min = 4, max = 4)
@Column(name = "CODE", unique = true, nullable = false)
private Integer code;

@Length 注解只能用于 String 而不是 Integer

【讨论】:

啊哈,我会试试这个。但是我怎样才能验证这个长度的整数呢? 我把你的问题放在谷歌上,这是第一个链接:@Range(min=, max=)

以上是关于找不到类型的验证器:java.lang.Integer的主要内容,如果未能解决你的问题,请参考以下文章

Java基础-final关键字分析

javax.servlet.ServletException:HV000030:找不到类型的验证器:java.util.Date [重复]

方法理论学习

找不到类验证器的元数据

在流明中找不到类“验证器”

在类路径中找不到休眠验证器