随笔 - 10, 文章 - 5, 评论 - 0, 阅读 - 7296
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SpringBoot 自定义内容协商策略 configureContentNegotiation

Posted on   zachry-r  阅读(453)  评论(0编辑  收藏  举报
  1. 在自定义的config配置类中,重写configureContentNegotiation方法
  2. @Bean
        public WebMvcConfigurer webMvcConfigurer(){
            return new WebMvcConfigurer() {
                /**
                 * 自定义内容协商策略
                 * @param configurer
                 */
                @Override
                public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                    // 自定义策略
                    // ContentNegotiationConfigurer mediaTypes(@Nullable Map<String, MediaType> mediaTypes)
                    Map<String, MediaType> map = new HashMap<>();
                    map.put("json", MediaType.APPLICATION_JSON);
                    map.put("xml", MediaType.APPLICATION_ATOM_XML);
                    map.put("gg",MediaType.parseMediaType("applicaion/x-z"));
                    // 指定基于参数的解析类型
                    ParameterContentNegotiationStrategy negotiationStrategy = new ParameterContentNegotiationStrategy(map);
                    // 指定基于请求头的解析
                    HeaderContentNegotiationStrategy headerContentNegotiationStrategy = new HeaderContentNegotiationStrategy();
                    configurer.strategies(Arrays.asList(negotiationStrategy));
                }
            };
        }

     

相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示