java - BindException thrown instead of MethodArgumentNotValidException in REST application -


I have a simple spring rest controller with some validation. I understand that verification failure will throw a MethodArgumentNotValidException though, my Code throws a BindException instead. In debug messages, I return the app to an empty model Endview.

Why does a rest controller throws BindException or an empty ModelAndView?

Note: I am testing my web application to curl and create an HTTP post

  curl -x post http: // localhost: 8080 / work   

I deliberately delete the "name" parameter is a required field marked with a @TotNull and @NotBlank annotation

My Controller:

  @RestController Public Sector Task Controller {Private Static Logger Logger = Logger. Tagologer (Efficient Controller Category); @Autowired Private MessageSource messageSource; @ Authorized Intermediary Verifier; Protecting the secure In-In-Binder (WebDetBinker Binder) @InInBinder {binder.setValidator (this.validator); } @RequestMapping (value = "/ task", method = RequestMethod.POST) public work createTask (@Valiv WorkGroup task comand) {task work = new task (); Task.setName (tasksCommand.getName ()); task.setDue (tasksCommand.getDue ()); Task.setCategory (tasksCommand.getCategory ()); Return work; }}   

My "command" class (which includes legal annotation)

  public class function command {@notbank} Name of the private personal string; Private calendar reason; Private string category; ... Gates & amp; Setters ommitted ...}   

My RestErrorHandler class:

  @ControllerAdvice public class RestErrorHandler {Personal Static Logger Logger = Logger.getLogger (RestErrorHandler.class) ; @Autowired Private MessageSource messageSource; @ExceptionHandler (MethodArgumentNotValidException.class) @ResponseStatus (HttpStatus.BAD_REQUEST) @ResponseBody Public Errors List Process Acrobators (MethodArgumentNotValidException Pre) {logger.info ("Error handlers apply ..."); Binding result result = ex.getBindingResult (); & Lt; FieldError & gt; FieldErrorList = result.getFieldErrors (); Error list errors = new errors (); (Field error field error: field error) {locale currentLocal = localContaxholder.titocale (); String error message = messageSource.getMessage (fieldError, currentLocale); Logger.info ("Adding Error Messages -" Error Message + "- For Errors"); errorsList.addFieldError (fieldError.getField (), error message); } Return errors; }}   

The processErrors method marked with @ExceptionHandler (...) is never called. If I try to capture a BindException using the @ExceptionHandler (...) annotation, then the handler method is used.

I have some support classes - Tasks, Task Commands, Error and Errors List - I can post if necessary code.

The problem was with my curl command.

The curl -d sends the content type "application / x-www-form-urlencoded" as a result, interpret spring data as web form data (instead of JSON). Using the Spring Form of HTTPS Message Convergence, the post office is converted into domain object and results are in the bidding option.

What we have to do is to use Spring Masoning 2 HTTPS Message Convercher to treat the PSO data as JSON and to parse the post office in the object. This can be done by specifying the "content-type" header with curl command:

  curl -x post-h "content-type: application / jason" -d '{"name ":" Name1 "," cause ":" 2014-DEC-31 01:00:00 PDT "," category ":" demo "} 'http: // localhost: 8080 / work   

To post this JSON data post using curls, refer to:

In addition to this, there are related spring documents about message converters: < / Html>

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -