title: how to return a custom spring error response with custom parameter annotations?
body: <p>I'm new to spring, so please excuse my ignorance. 
  I'm trying to "return" a custom response when a given method parameter, which is "required", is null. 
  the current response, from spring, is: </p>
  
  <pre><code>{
    "timestamp": 1477060294961,
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.web.bind.MissingServletRequestParameterException",
    "message": "Required String parameter 'bookname' is not present",
    "path": "/getbook"
  }
  </code></pre>
  
  <p>I am trying to get to a point where it "returns": </p>
  
  <pre><code>{
    "status": 400,
    "error": {
      // custom error body
    }
  }
  </code></pre>
  
  <p>I thought a nice way of doing this would be to have a custom "parameter annotation". This would also make the code much more readable and store useful information about this endpoint parameter. </p>
  
  <p>I followed the example given <a href="https://stackoverflow.com/questions/30715579/custom-spring-annotation-for-request-parameters">here</a>, but I'm not sure where or how to return the custom response?</p>
  
  <p>So far I have the annotation:  </p>
  
  <pre><code>@Target(ElementType.PARAMETER)
  @Retention(RetentionPolicy.RUNTIME)
  @Component
  public @interface customParameter {
  
  String value() default "";
  boolean required() default false;
  String defaultValue() default ValueConstants.DEFAULT_NONE;
  String customInfo() default  "blar blar";
  }
  </code></pre>
  
  <p>the "endpoint" : </p>
  
  <pre><code>  @RequestMapping(value = "/getbook", method = {RequestMethod.POST})
    public ResponseEntity&lt;BookResponse&gt; getBookInfo(
        @customParameter(value = "bookname", required = true, customInfo = "extremely valuable book")
        final String bookname
     ) {
      return new bookRequest(bookname).getResponse;
  }
  </code></pre>
  
  <p>and have an custom resolver :</p>
  
  <pre><code>public class CustomAnnotationResolver implements HandlerMethodArgumentResolver {
  
  
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
      return parameter.getParameterAnnotation(customParameter.class) != null;
    }
  
  
    @Override
    public Object resolveArgument(MethodParameter methodparameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
  
      CustomParameter customParameter = methodparameter.getParameterAnnotation(CustomParameter.class);
  
      String parameter = webRequest.getParameter(CustomParameter.value());
  
      // todo: do Validation here
      if (customParameter == null) {
        if (Parameter.required()) {
          String customInfo = customParameter.customInfo();
          String body getBody(customInfo);
          new ResponseEntity(body, 400); // so the problem is here!! how do I return this response??  
        }
      }
  
      return webRequest.getParameter(customParameter.value());
  
    }
  }
  </code></pre>
  
  <p>I have also "registered" this resolver with the webConfig : </p>
  
  <pre><code>@EnableWebMvc
  @Configuration
  public class WebConfig extends WebMvcConfigurerAdapter {
  
    @Override
    public void addArgumentResolvers(List&lt;HandlerMethodArgumentResolver&gt; argumentResolvers) {
      argumentResolvers.add(new CustomAnnotationResolver());
    }
  }
  </code></pre>
  
  <p>any help with this implementation or any other suggestions of how to do this would be fantastic. Thanks all :)</p>