How do I throw an error if extra parameters are specified in the JSON request? For example, "xxx" is not a valid parameter or in the @RequestBody
object.
$ curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"apiKey": "'$APIKEY'", "email": "name@example.com", "xxx": "yyy"}' localhost:8080/api/v2/stats
I tried adding @Validated
to the interface, but it didn't help.
@RequestMapping(value = "/api/v2/stats", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<DataResponse> stats(Principal principal, @Validated @RequestBody ApiParams apiParams) throws ApiException;
I would like to enable a 'strict' mode so that it will give an error if extra, spurious parameters exist in the request. I could find no way to do this. I found ways to ensure the valid parameters do exist, but no way to ensure there are not extra parameters.
public class ApiParams extends Keyable {
@ApiModelProperty(notes = "email of user", required = true)
String email;
public abstract class Keyable {
@ApiModelProperty(notes = "API key", required = true)
@NotNull
String apiKey;
Spring Boot 1.5.20
Behind the scene, Spring uses the Jackson library to serialize/deserialize POJO to JSON and vice versa. By default, the
ObjectMapper
that the framework uses to perform this task has itsFAIL_ON_UNKNOWN_PROPERTIES
set tofalse
.You can turn this feature on GLOBALLY by setting the following config value in
application.properties
.Subsequently, if you want to ignore unknown properties for specific POJO, you can use the annotation
@JsonIgnoreProperties(ignoreUnknown=true)
in that POJO class.Still, this is a lot of manual work going forward. Technically, ignoring those unexpected data doesn't violate any software development principles. There might be scenarios where there's a filter or servlet sitting in front of your
@Controller
doing additional stuff that you're not aware of which requires those extra data. Does it worth the effort?I found a solution here: https://stackoverflow.com/a/47984837/148844
I added this to my Application.
The
@JsonIgnoreProperties
was not needed. Now it returns an error like(I happen to have a
@ControllerAdvice
classResponseEntityExceptionHandler
.)You could try providing a custom implementation for 'MappingJackson2HttpMessageConverter ' class for this message conversion.
Now only we need to register the Custom MessageConverter with the spring context.In the configuration class.Below is the code
Hope that helps ..
I know this is not the best solution but still, I'm posting it.
You can implement
Interceptor
for your controller URL. InpreHandle
method of your interceptor, you will be able to getHttpServletRequest
object from which you can get all the request parameters. In this method, you can write code to write strict validation for request parameters and throw an exception for invalid parameters present in your request.You can write the validation code in your controller class as well by getting
HttpRequest
object in your Controller method but it would be good to keep your controller logic and validation logic in separate space.Interceptor :
You should also look at the answers given in How to check for unbound request parameters in a Spring MVC controller method? as well.