SpringBoot 自定義內容協商策略 configureContentNegotiation
阿新 • • 發佈:2022-02-17
- 在自定義的config配置類中,重寫configureContentNegotiation方法
-
@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)); } }; }