6

Our controller looks like this -

@RestController
@RequestMapping(value="api")
@Validated
public class SampleController {
@RequestMapping(value = {"/test"}, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public void test(
        @RequestParam(value = "testCode",required=true) String merchantCode
        ) throws Exception{
    System.out.print("This is Test");
}
}

Here if we do not specify the required parameter "testCode" in our request we get "400 Bad Request", but the message section of the error response remains blank.

Response we are getting -

{"timestamp":1592441286607,"status":400,"error":"Bad Request","message":"","path":"/test"}

But expected is -

{"timestamp":1592441286607,"status":400,"error":"Bad Request","message":"Required String parameter 'testCode' is not present","path":"/test"}

We are using below Spring dependencies -

<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-framework.version>5.2.6.RELEASE</spring-framework.version>

What I have seen is for this we are getting MissingServletRequestParameterException, but in the exception the message is coming as blank("").

Arijit Roy
  • 379
  • 3
  • 14
  • If you want a custom message you will probably have to create your custom validator. Take a look here https://stackoverflow.com/questions/36823263/custom-validation-for-requestparam-doesnt-work-with-spring-mvc – Federico Piazza Jun 22 '20 at 16:29
  • No, not custom message actually. What I am saying is the message section is blank it self. Am expecting default message here. – Arijit Roy Jun 22 '20 at 16:36
  • The same code is working in spring boot version 2.2.6 – Nayan Jun 23 '20 at 14:13

2 Answers2

7

I just updated the bootstrap.yml with server.error.include-message=always. It appears from Spring 2.3.0 the default behavior of Spring has changed. We can refer the following link from more details https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#changes-to-the-default-error-pages-content

Arijit Roy
  • 379
  • 3
  • 14
1

Configuration in application.yml

server:
  error:
    include-message: always
    include-binding-errors: always
    include-stacktrace: on_trace_param
    include-exception: true
akasha
  • 494
  • 6
  • 4