spring-mvc - Spring Boot 中的 WebMvcConfigurationSupport 替换是什么?

标签 spring-mvc spring-boot content-negotiation

在传统的 Spring MVC 中,我可以扩展 WebMvcConfigurationSupport并执行以下操作:

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false).
    favorParameter(true).
    defaultContentType(MediaType.APPLICATION_JSON).
    mediaType("xml", MediaType.APPLICATION_XML);
}

如何在 Spring Boot 应用程序中执行此操作?我的理解是添加一个 WebMvcConfigurationSupport@EnableWebMvc将禁用我不想要的 Spring Boot WebMvc 自动配置。

最佳答案

每个 Spring Boot 引用 auto configuration and Spring MVC :

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Bean of type WebMvcConfigurerAdapter, but without @EnableWebMvc.



例如,如果要保留 Spring Boot 的自动配置,并自定义 ContentNegotiationConfigurer:
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

   ...
   @Override
   public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
      super.configureContentNegotiation(configurer);
      configurer.favorParameter(..);
      ...
      configurer.defaultContentType(..);
   }
}

关于spring-mvc - Spring Boot 中的 WebMvcConfigurationSupport 替换是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37130332/

相关文章:

java - WEB-INF/jsp 中包含更多文件夹的 viewResolver 在 spring 中不起作用

rest - 如果内容协商失败,正确的后备措施是什么?

java - 将嵌套 json 映射到 java 字段,无需写入容器名称

java - 添加 spring-boot-starter-parent 会删除 javax.validation.*

grails - Grails内容协商-处理不受支持的类型

grails - 有没有更简单的方法可以在Grails中进行内容协商的响应?

java - 使用 modelAttribute 提交表单时在 Spring MVC 中上传多个文件时出现 400 错误请求

java - 在 Spring MVC 中使用 Ajax 和 SimpleFormController

java - 使用 Spring MVC,接受带有错误 JSON 的 POST 请求会导致返回默认的 400 错误代码服务器页面

spring - 使用 componentModel = "spring"的 Mapstruct 依赖注入(inject)给出空对象