0

I am trying to validate both header and body of a post request using the below code

@PostMapping(path = "/shelloo", consumes = "application/json")
public String ssfirst(@Valid @RequestHeader("username") String username, BindingResult headerBindingResult,
            @ModelAttribute RequestBodyModel requestBodyModel, BindingResult bodyBindingResult) {
    if (headerBindingResult.hasErrors())
        System.out.println("found header error");
    else if (bodyBindingResult.hasErrors())
        System.out.println("found body error");
    else
        System.out.println("else Print");
    return "sample response";
}

I am getting Exception as follow

 java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply: public java.lang.String com.jvr.middlewareuat.rest.MainController.ssfirst(java.lang.String,org.springframework.validation.BindingResult,com.jvr.middlewareuat.model.RequestBodyModel,org.springframework.validation.BindingResult)
  • 2
    It seems like this question is duplicate to this thread: https://stackoverflow.com/questions/18646121/an-errors-bindingresult-argument-is-expected-to-be-declared-immediately-after-th – Smeki Jan 05 '22 at 08:34
  • What is a valid `String`? The `@Valid` there doesn't make any sense. – M. Deinum Jan 05 '22 at 09:15
  • I am trying to validate the header key (username) and the body of the request. If its wrong can you suggest a better way to do it – Vishnu Jr Feb 02 '22 at 11:12

0 Answers0