欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot报406,web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

程序员文章站 2022-06-01 15:26:16
...

      SpringBoot菜鸡最近使用@ResponseBody返回一个map集合,集合包含数据总条数及数据集合list,方法如图:

SpringBoot报406,web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

      结果运行时断点进了后台但是后台报错,异常信息为:

WARN (AbstractHandlerExceptionResolver.java:189)- Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation。

      同时前台报错406,错误信息如图:

SpringBoot报406,web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

      于是菜鸡我寻求度娘,在试过多位大神给出的解决方案后,终于解决这个问题,其实就是让SpringBoot启动主方法继承一下WebMvcConfigurerAdapter类并重写其configureContentNegotiation方法。

        继承前的Application如图:

SpringBoot报406,web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

 

        继承后正常运行的Application如图:

SpringBoot报406,web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

        代码如下:

public class Application extends WebMvcConfigurerAdapter{
   //favorPathExtension表示是否支持后缀匹配
   @Override
   public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
         configurer.favorPathExtension(false);
   }
   public static void main(String[] args) {
      SpringApplication.run(Application.class,args);
   }

}

      码字不易,转载请注明出处!