Home > other >  Bad Request response with name of missing field - Spring Boot
Bad Request response with name of missing field - Spring Boot

Time:03-17

I have an API endpoint that get a name and description parameters (both are mandatory)

createSomething(@RequestParam(value = "name") String name,@RequestParam(value = "description") String description)

If the client is not providing any of these he will get 400 Bad Request

Is there a way for me to tell the client which field is missing ? give more information for the "Bad Request" response

CodePudding user response:

You can use validation with a customised message :

@GetMapping("/name-for-month")
public String getNameOfMonthByNumber(@RequestParam @Min(1) @Max(value = 12, message = “month number has to be less than or equal to 12”) Integer month) {
// ...
}

CodePudding user response:

There are many ways of handling errors for Rest find below a link of at least 5 solutions for your issue :

  1. ExceptionHandler
  2. HandlerExceptionResolver (ResponseStatusExceptionResolver this is the most adducate for your case or the 4th one if you use spring 5 )
  3. ControllerAdvice
  4. ResponseStatusException
  5. Handle the Access Denied in Spring Security

https://www.baeldung.com/exception-handling-for-rest-with-spring

CodePudding user response:

Since both parameters are mandatory you'll be getting 400 (bad request) if you try to send the request without paramters.

A workaround could be making request parameters non-mandatory(so that request can be sent without parameters) and provide a default value in case no parameter is provided

createSomething(@RequestParam(value = "name", required=false, defaultValue = null) String name,@RequestParam(value = "description", required=false, defaultValue = null) String description)

In the function, you can check for null like the following -

if (name == null) // name parameter is not provided
if (description == null) // description paramter is not provided

And, based on conditions you can also send error reponse if any one/more paramter not provided in the request.

CodePudding user response:

I see multiple answers but no one is specific enough.

1)

Spring by default has the capability of reporting in the error message what parameter was missing or other violations in the request.

However since spring boot version 2.3 the specific error messages are hidden, so that no sensitive information can be disclosed to the user.

You can use the property server.error.include-message: always which was the default mechanism before 2.3 version and allow spring to write error messages for you again.

2)

If you can't afford this because other sensitive info could be leaked from other exceptions, then you have to provide your own exception handler for this specific case

The following can be placed either in the same controller, or in another class marked with @ControllerAdvice

@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity handleMissingParams(MissingServletRequestParameterException ex) {
    return ResponseEntity.badRequest().body(String.format("Missing parameter with name:%s", ex.getParameterName()));
}

CodePudding user response:

Probably you will also need to enable the application properties

server.error.include-message=always

More properties for reference here.

  • Related