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;
}
}