spring - 400 Hibernate @Valid 错误请求

标签 spring hibernate validation tomcat hibernate-validator

当我验证我的表单时,我有一个奇怪的行为。

一旦我添加了 Hibernate @Valid 注释,如果发布的数据无效,Tomcat 就会将我的请求视为“错误”。如果数据有效,不用担心。

我使用:

  • Tomcat 7.0.52
  • Javax 验证 api 1.1.0.Final
  • Hibernate validator 5.1.0.Final
  • Spring 4.0.3.RELEASE

目前,我做了一个非常简单的验证:

public class RemoveCacheElementForm {

    @NotBlank(message = "Please select a cache name.")
    private String name;

    @NotBlank(message = "Please select a cache entry key.")
    private String key;

Spring Controller :

/**
 * Handler to remove one cached elements from the specified cache.
 * 
 * @return the view.
 */
@RequestMapping(value = CACHE_REMOVE, method = RequestMethod.POST)
public String removeCachedElement(ModelMap model, @Valid @ModelAttribute(FORM_NAME) RemoveCacheElementForm form) {
    model.addAttribute("removeElementResult", CacheUtils.removeCachedElement(form.getName(), form.getKey()));
    initializeModel(model);
    return CACHE_ADMIN_PAGE;
}

当我删除@Valid 注释时,也不用担心。

有人有想法吗?

非常感谢您的帮助! :-)

最佳答案

尝试将您的代码更改为

@RequestMapping(value = CACHE_REMOVE, method = RequestMethod.POST)
public String removeCachedElement(ModelMap model, @Valid @ModelAttribute(FORM_NAME) RemoveCacheElementForm form, BindingResult bindingResult) {
    model.addAttribute("removeElementResult", CacheUtils.removeCachedElement(form.getName(), form.getKey()));
    initializeModel(model);
    return CACHE_ADMIN_PAGE;
}

关于spring - 400 Hibernate @Valid 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23202788/

相关文章:

java.security.AccessControlException : access denied ("java.lang.RuntimePermission" "modifyThreadGroup")

java - JPA/hibernate : How to associate composite foreign keys with partial primary keys

Hibernate:与同一类的多个关联

javascript - jQuery:检查包含特定值的表行是否已经存在

java - 在处理之前让 SpringBoot API 返回 200

java - 无法从Spring云配置服务器中的GIT读取属性源

C++ 用户输入限制,正确重试,没有 "goto"

javascript - 添加查询字符串时验证不起作用

java - spring WebApplicationInitializer 配置多个servlet

java - tomcat中PostgreSQL数据库的JNDI查找