1.在配置文件中开启参数请协商(默认是关闭的)
2.在WebMvcConfigurer配置类中自定义内容协商策略
@Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { HashMap<String, MediaType> MediaTypeHashMap = new HashMap<>(); MediaTypeHashMap.put("xml",MediaType.APPLICATION_XML);//自定义参数协商规则 MediaTypeHashMap.put("json",MediaType.APPLICATION_JSON); MediaTypeHashMap.put("wang",MediaType.parseMediaType("application/wang")); ParameterContentNegotiationStrategy Strategy = new ParameterContentNegotiationStrategy(MediaTypeHashMap);//生成策略 /** * 将自定义策略加入配置,由于这里重写覆盖了原有容器中的策略因此我们还要把默认的基于请求头的策略加入配置 */ configurer.strategies(Arrays.asList(Strategy, new HeaderContentNegotiationStrategy())); }
3.在浏览器发送请求指定返回形式为自定义的application/wang类型,关于自定义响应类型参考上一篇文章自定义messageConvert