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