knife4j-spring-boot-starter doc.html 404 解决


application.yml配置如下

  mvc:
    static-path-pattern: /static/**

mvc 配置如下【/doc.html】

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/doc.html").addResourceLocations("classpath*:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath*:/META-INF/resources/webjars/");
}

添加如下controller

package com.laolang.shop.common.swagger;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
public class SwaggerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/static/swagger-resources/configuration/ui")
    public String ui() {
        String forObject = restTemplate.getForObject("http://localhost:8091/swagger-resources/configuration/ui", String.class);
//        log.info("[Knife4j] - /static/swagger-resources/configuration/ui:\n{}", JSONUtil.toJsonPrettyStr(forObject));
        return forObject;
    }

    @GetMapping("/static/swagger-resources")
    public String resources() {
        String forObject = restTemplate.getForObject("http://localhost:8091/swagger-resources", String.class);
//        log.info("[Knife4j] - /static/swagger-resources:\n{}", JSONUtil.toJsonPrettyStr(forObject));
        return forObject;
    }

    @GetMapping("/static/v2/api-docs")
    public String apiDocs(@RequestParam(value = "group", required = false) String group) {
        String url = "http://localhost:8091/v2/api-docs";
        if (StrUtil.isNotBlank(group)) {
            url = url + "?group=" + group;
        }
        String forObject = restTemplate.getForObject(url, String.class);
//        log.info("[Knife4j] - /static/v2/api-docs:\n{}", JSONUtil.toJsonPrettyStr(forObject));
        return forObject;
    }

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM