亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Boot內容協商配置

Spring Boot內容協商配置

滄海一幻覺 2021-05-11 13:30:55
我在使用spring-boot配置內容協商時遇到困難。我想保留大多數默認的spring-boot配置。我遵循了以下 https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc/, 而不是最近的教程。目前,當我發送application/json或txt/html的請求時,視圖似乎沒有解決,但是當我打開視圖時,@EnableWebMvc它似乎得到了解決。以下是我當前的配置。@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 configurationpublic 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;    }}
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

您沒有在內容協商管理器中注冊解析器。


請嘗試以下修改:


@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;

}


查看完整回答
反對 回復 2021-05-26
  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號