I'm doing this strict schema validation using an interceptor (so I can get the InputStream value from the HttpServletRequest class).
public class RequestInterceptor extends HandlerInterceptorAdapter {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception)
throws Exception {
// Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception {
// Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
InputStream parameters = request.getInputStream();
final ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
switch (request.getContentType()) {
case "V0":
try {
mapper.readValue(parameters, new TypeReference<JsonApi<CountryVo>>() {});
}catch (final UnrecognizedPropertyException e) {
throw new UnrecognizedRequestException();
}
case "V1":
default:
try {
mapper.readValue(parameters,
new TypeReference<JsonApi<CountryVoV1>>() {
});
} catch (final UnrecognizedPropertyException e) {
throw new UnrecognizedRequestException();
}
}
return true;
}
}
What I'm doing in this is just checking the expect DTO, based on the content-type the user sent, and then if it's wrong, I expect it to throw the UnrecognizedPropertyException.
The problem with this is that later on it throws Required request body is missing - for the right payloads. When I stop using this interceptor, the problem goes away.