使用jackson增加全局序列化方式,校验所有接口入参,判断是否包含多余入参,比如原本后端接收的入参为:
java">{
"name":"张三",
"age":"22",
"high":"183"
}
如果前端传参多了其他的字段,比如:
java">{"name":"张三","age":"22","high":"183","weight":"160"
}
就会被下面的拦截处理给拦截然后报错。
java">@Configuration
@RefreshScope
public class WebConfigurer extends WebMvcConfigurationSupport {
@Value("${ultra.jackjson.status}")private String status;
@Beanpublic AuthenticationInterceptor authenticationInterceptors() {return new AuthenticationInterceptor();}
/*** 请求前拦截处理** @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(authenticationInterceptors()).addPathPatterns("/**");super.addInterceptors(registry);}
@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}
/*** 全局序列化方式** @param converters*/@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {//Jackson的全局序列化方式if ("open".equals(status)){configureJacksonHttpMessageConverter(converters);}}/*** Jackson的全局序列化方式** @param converters*/private void configureJacksonHttpMessageConverter(List<HttpMessageConverter<?>> converters) {MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();ObjectMapper objectMapper = new ObjectMapper();//反序列化的时候如果多了其他属性,不抛出异常objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);converter.setObjectMapper(objectMapper);converters.add(converter);converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));}
}