在Controller中为每个RequestMapping配置FAIL_ON_UNKNOWN_PROPERTIES [英] Configure FAIL_ON_UNKNOWN_PROPERTIES for each RequestMapping differently in the Controller

查看:632
本文介绍了在Controller中为每个RequestMapping配置FAIL_ON_UNKNOWN_PROPERTIES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在控制器中的不同@RequestMapping上以不同方式处理json到Object的转换.

I want to handle json to Object conversion differently on different @RequestMapping in my Controller.

我相信,如果我们在spring-boot项目中添加Jackson依赖项,它将处理json到Object的转换,并且#spring.jackson.deserialization.fail-on-unknown-properties=true属性将确保如果json中存在一些未知的属性,转换将失败(如果我是错误的.)

I believe if we add Jackson dependency in our spring-boot project it handles json to Object conversion and #spring.jackson.deserialization.fail-on-unknown-properties=true property will make sure that conversion will fail if there is some unknown property present in the json (please correct me if I am wrong).

我们可以在本地告诉杰克逊何时在未知属性上失败,什么时候忽略那些属性.

以下是使用标记的代码段.

Following is code snippet to use a flag.

    @GetMapping(value = "sample")
    public @ResponseBody UserDTO test(@RequestParam String str, @RequestParam boolean failFast) {
        ObjectMapper map = new ObjectMapper();
        if( failFast) {
            map.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
        } else {
            map.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        }
        UserDTO userDTO = null;
        try {
            userDTO = map.readValue(str, UserDTO.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return userDTO;
    }

我不需要像运行@RequestParam.那样在运行时对其进行处理 有一些属性可以用来标记映射,在哪里可以检查未知属性,在哪里可以忽略它们.

I don't need it to be handled at runtime like i am doing using @RequestParam. Is there some property using which i can use to mark mappings where to check for unknown properties and where to ignore them.

修改: 我正在寻找的是更改现有应用程序以处理每个映射的未知属性.例如:

Edit: What I am looking for is to change an existing application to handle Unknown property per mapping. For example:

        @PostMapping(value = "fail/fast")
        public @ResponseBody UserDTO test(@FAIL_ON_UNKNOWN @RequestBody UserDTO userDTO, @RequestParam boolean failFast) {
            ..///processing...
            return userDTO;
        }

        @PostMapping(value = "fail/safe")
        public @ResponseBody UserDTO test( @RequestBody UserDTO userDTO, @RequestParam boolean failFast) {
                ..///processing...
                return userDTO;
        }

如果可以为每个映射添加一些验证之王,那么我不需要更改所有现有映射来自定义未知属性,并且代码更改将最少.

If some king of validation can be added per mapping then i don't need to change all existing mapping's to customise unknown property and code change will be minimum.

推荐答案

我能够通过实现自己的 HttpMessageConverter 获得所需的结果.感谢 @MichalZiober 的建议.

I was able to achieve the desired result by implementing my own HttpMessageConverter. Thanks to @MichalZiober for suggesting it.

我创建了一个自定义HttpMessageConvertor,并使用我的自定义MediaType:{"application", "json-failFast"}注册了它.

I created a Custom HttpMessageConvertor and registered it with my custom MediaType:{"application", "json-failFast"}.

只要存在 Header: Content-Type:application/json-failFast ,这是怎么工作的,那么从json转换为Object和 @RequestBody/@ResponseBody 中的未知属性将不被接受> .

并且只要存在 Header: Content-Type:application/json ,就会忽略 @RequestBody/ResponseBody 中无法识别的属性.

And whenever Header: Content-Type:application/json is present then unrecognised properties in @RequestBody/ResponseBody will be ignored.

这是我的自定义 HttpMessageConverter :

@Component
public class CustomJsonMessageConverter extends AbstractJackson2HttpMessageConverter {

    @Nullable
    private String jsonPrefix;

    public CustomJsonMessageConverter() {
        this(Jackson2ObjectMapperBuilder.json().build().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,true));
    }
    public CustomJsonMessageConverter(ObjectMapper objectMapper) {
        super(objectMapper, new MediaType[]{ new MediaType("application", "json-failFast")});
    }

    public void setJsonPrefix(String jsonPrefix) {
        this.jsonPrefix = jsonPrefix;
    }

    public void setPrefixJson(boolean prefixJson) {
        this.jsonPrefix = prefixJson ? ")]}', " : null;
    }

    protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
            if (this.jsonPrefix != null) {
            generator.writeRaw(this.jsonPrefix);
        }
    }
}

这篇关于在Controller中为每个RequestMapping配置FAIL_ON_UNKNOWN_PROPERTIES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆