0


Knife4j文档请求异常(基于SpringBoot3,查找原因并解决)

博主采用的是SpringBoot3+knife4j的框架,如果你采用的是SpringBoot2框架,那么可以看下面这几篇:

Knife4j文档请求异常 | Knife4j

记一次集成swagger2(Knife4j)在线文档异常,提示:Knude4j文档请求异常_knife4j文档请求异常-CSDN博客全网多种方式解决Knife4j文档请求异常-CSDN博客


博主最近做苍穹外卖时碰到了一个问题,Knife4j文档请求异常,如下图所示:

于是F12打开控制台准备进行debug,发现报出如下错误:

大致意思是JSON返回了一个token,继续从网络的响应查找:

发现响应码(200)正常,并且返回格式为json,查看响应内容:

居然是一长串字符串,而不是Json数据。于是到了最令人烦躁的debug环节,从编辑器控制台查看没有任何ERROR甚至WARN信息:

2024-01-09T15:17:43.508+08:00  INFO 22868 --- [           main] com.sky.SkyApplication                   : Starting SkyApplication using Java 17.0.8 with PID 22868 (E:\Code\sky_take_out_proj\sky_take_out\sky_server\target\classes started by User in E:\Code\sky_take_out_proj\sky_take_out)
2024-01-09T15:17:43.510+08:00  INFO 22868 --- [           main] com.sky.SkyApplication                   : The following 1 profile is active: "dev"
2024-01-09T15:17:44.228+08:00  INFO 22868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2024-01-09T15:17:44.230+08:00  INFO 22868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-01-09T15:17:44.265+08:00  INFO 22868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 23 ms. Found 0 Redis repository interfaces.
2024-01-09T15:17:44.830+08:00  INFO 22868 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2024-01-09T15:17:44.837+08:00  INFO 22868 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-01-09T15:17:44.837+08:00  INFO 22868 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.16]
2024-01-09T15:17:44.920+08:00  INFO 22868 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-01-09T15:17:44.920+08:00  INFO 22868 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1369 ms
Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts
2024-01-09T15:17:45.242+08:00  INFO 22868 --- [           main] com.sky.config.WebMvcConfiguration       : 开始注册自定义拦截器...
2024-01-09T15:17:45.908+08:00  INFO 22868 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2024-01-09T15:17:45.917+08:00  INFO 22868 --- [           main] com.sky.SkyApplication                   : Started SkyApplication in 3.027 seconds (process running for 3.722)
2024-01-09T15:17:45.919+08:00  INFO 22868 --- [           main] com.sky.SkyApplication                   : server started
2024-01-09T15:19:13.412+08:00  INFO 22868 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-01-09T15:19:13.413+08:00  INFO 22868 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-01-09T15:19:13.414+08:00  INFO 22868 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
2024-01-09T15:19:14.040+08:00  INFO 22868 --- [nio-8080-exec-6] o.springdoc.api.AbstractOpenApiResource  : Init duration for springdoc-openapi is: 261 ms

突然想起来之前设置过一个消息转换器,将Date类型转换为yyyy-MM-dd HH:mm格式去显示,于是考虑是否因为消息转换器,将JSON数据不正常解析了?

    /**
     * 扩展消息转换器,将日期类型从列表转换为时间戳
     * @param converters 消息转换器列表
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());

        converters.add(0,jackson2HttpMessageConverter);
    }

于是将消息转换器注释掉,便正常显示了:

返回的JSON数据也正常了,代价是后端返回的时间数据不是上面提到的格式,而是列表,导致前端显示错误的格式:

至此


如果你还想保留消息转换器,让后端返回正确的时间数据格式,那么可以调整消息转换器列表中转换器的顺序,代码如下。

    /**
     * 扩展消息转换器,将日期类型从列表转换为时间戳
     * 这个是导致knife4j不能正常显示的罪魁祸首,特别要注意添加的位置
     * @param converters 消息转换器列表
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
        converters.add(converters.size()-1,jackson2HttpMessageConverter);
    }

一些参考文章:

MappingJackson2HttpMessageConverter使用及jackson配置原理和避坑说明-CSDN博客

一步到位 SpringBoot 序列化与消息转换器 (你需要的这里都有)_springboot 序列化 自定义转换器-CSDN博客

为什么说HttpMessageConverter的顺序非常重要_SpringBoot

标签: java bug 后端

本文转载自: https://blog.csdn.net/nofaliure/article/details/135481301
版权归原作者 Jason Steve 所有, 如有侵权,请联系我们删除。

“Knife4j文档请求异常(基于SpringBoot3,查找原因并解决)”的评论:

还没有评论