Abh*_*kar 4 spring-mvc content-negotiation spring-boot
在传统的Spring MVC中,我可以扩展WebMvcConfigurationSupport并执行以下操作:
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).
favorParameter(true).
defaultContentType(MediaType.APPLICATION_JSON).
mediaType("xml", MediaType.APPLICATION_XML);
}
Run Code Online (Sandbox Code Playgroud)
我如何在Spring Boot应用程序中执行此操作?我的理解是,增加了WebMvcConfigurationSupport与@EnableWebMvc将禁用春季启动WebMvc自动配置,这是我不想要的.
关于自动配置和Spring MVC的 Per Spring Boot参考:
如果您想完全控制Spring MVC,可以使用@EnableWebMvc添加自己的@Configuration注释.如果你想保持弹簧引导MVC功能,而你只是想添加额外的MVC配置(拦截器,格式化,视图控制器等),你可以添加型WebMvcConfigurerAdapter的自己@Bean,但没有@EnableWebMvc.
例如,如果要保留Spring Boot的自动配置,并自定义ContentNegotiationConfigurer:
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
...
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
super.configureContentNegotiation(configurer);
configurer.favorParameter(..);
...
configurer.defaultContentType(..);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6071 次 |
| 最近记录: |