400 Bad request with Hibernate @Valid

I have strange behavior when I check my form.

As soon as I add the Hibernate @Valid annotation, Tomcat agreed to my request as "bad" if the published data is invalid. If the data is valid, do not worry.

I use:

  • Tomcat 7.0.52
  • Javax Validation api 1.1.0.Final
  • Hibernate Validator 5.1.0.Final
  • Spring 4.0.3.RELEASE

At the moment, I am really doing a simple check:

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

When I delete the @Valid annotation, don't worry.

Does anyone have an idea?

Many thanks for your help!: -)

+4
source share
1 answer

Try changing your code to

@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;
}
+9

Source: https://habr.com/ru/post/1537399/


All Articles