首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Boot内容-协商配置

Spring Boot内容-协商配置
EN

Stack Overflow用户
提问于 2018-07-15 08:55:39
回答 1查看 3.6K关注 0票数 1

我很难配置内容协商与spring启动。我想保留大多数默认的弹簧引导配置。我遵循了下面的https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc/ --不是最近的教程。当我向application/jsontxt/html发送请求时,视图似乎没有得到解决,但当我打开@EnableWebMvc时,它似乎得到了解决。下面是我的当前配置。

代码语言:javascript
复制
@Configuration // according to the spring-boot docs this should be enough with spring-boot
//@EnableWebMvc  If I enable this content-negotiation seems to work without any configuration, but I loose the default spring-boot configuration
public class MvcConfiguration implements WebMvcConfigurer {


    @Bean(name = "jsonViewResolver")
    public ViewResolver getJsonViewResolver() {
        return new JsonViewResolver();
    }


    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    // Simple strategy: only path extension is taken into account
        configurer.favorPathExtension(true)
            .defaultContentType(MediaType.TEXT_HTML)
            .mediaType("html", MediaType.TEXT_HTML)
            .mediaType("json", MediaType.APPLICATION_JSON);
}

    @Bean
    public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
        ContentNegotiatingViewResolver resolver = newContentNegotiatingViewResolver();
        resolver.setContentNegotiationManager(manager);
        return resolver;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-11 18:40:53

您没有在内容协商管理器中注册解析器。

请尝试以下修改:

代码语言:javascript
复制
@Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager){
  ContentNegotiatingViewResolver resolver = newContentNegotiatingViewResolver();
  resolver.setContentNegotiationManager(manager);
  List<ViewResolver> resolvers = new ArrayList<>();
  ViewResolver aViewResolver = getJsonViewResolver();
  resolvers.add(aViewResolver);
  resolver.setViewResolvers(resolvers);
  return resolver;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51346834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档