zoukankan      html  css  js  c++  java
  • swagger2报错Unable to render this definition

    前言

    这个问题是第二次遇到了,索性就记录下来;

    问题描述

    Unable to render this definition
    The provided definition does not specify a valid version field.
    Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).

    解决办法

    查看接口返回api-docs信息

    控制台报错:system.js:461 TypeError: Cannot convert undefined or null to object

    接口api-docs返回信息未正确解析导致;

    添加mvc配置

    @Configuration
    @EnableWebMvc
    public class MvcConfiguration implements WebMvcConfigurer {
    
        @Bean
        public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
            converter.setObjectMapper(objectMapper());
            return converter;
        }
    
        @Bean
        ObjectMapper objectMapper(){
            ObjectMapper om = new ObjectMapper();
            om.setSerializationInclusion(JsonInclude.Include.ALWAYS);
            // 设置 SerializationFeature.FAIL_ON_EMPTY_BEANS 为 false
            om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return om;
        }
    
        /**
         * 防止@EnableMvc把默认的静态资源路径覆盖了,手动设置的方式
         *
         * @param registry
         */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            // 解决静态资源无法访问
            registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
            // 解决swagger无法访问
            registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            // 解决swagger的js文件无法访问
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }
    
  • 相关阅读:
    Nginx在linux环境下(centos7)的安装、负载均衡设置
    ocr识别开源软件tesseract试用记录
    Nginx在windows环境下的安装、负载均衡设置
    一个测试程序迭代的故事05
    一个测试程序迭代的故事04
    一个测试程序迭代的故事03
    一个测试程序迭代的故事02
    一个测试程序迭代的故事01
    Delphi5和Delphi7属性编辑器内存泄漏问题的解决
    使用Calibre自带工具批量转换电子书格式
  • 原文地址:https://www.cnblogs.com/tinging/p/13262378.html
Copyright © 2011-2022 走看看