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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34725588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:14:18  来源:igfitidea点击:

No validator could be found for type: java.lang.Integer

javaspringhibernatespring-securityhibernate-mapping

提问by Rafael

Trying to process my create/edit form I get the following error:

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

HTTP Status 500 - Request processing failed; nested exception is javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.

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

My Controller:

我的控制器:

/**
 * 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";

}

My model class:

我的模型类:

@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();
    }

Any suggestions?

有什么建议?

回答by user3495816

I believe problem is with this:

我相信问题在于:

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

@Lengthannotation should be used only for String not Integer

@Length注释应该只用于字符串而不是整数