1

我将 Swagger-UI 安装为独立的,由 nginx 服务器提供服务。

我们有几个微服务,我正在尝试从我们的 spring-boot 应用程序中实现 swagger json。

因此,如果我理解正确,我需要将 swagger-ui 指向我的应用程序的 swagger 端点的 url,以便接收该 json 信息。

我是怎么做到的?

在毕业典礼上:

dependencies {
    compile("com.mangofactory:swagger-springmvc:0.8.8")
}

为我的应用添加了新配置:

@Configuration
@EnableSwagger
public class SwaggerConfig {
    private SpringSwaggerConfig springSwaggerConfig;
    @Autowired


    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
            this.springSwaggerConfig = springSwaggerConfig;
        }
        @Bean
    // Don't forget the @Bean annotation
 public SwaggerSpringMvcPlugin customImplementation() {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(
                apiInfo()).includePatterns("/saurzcode/.*");
    }
    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo("SaurzCode API", "API for Saurzcode",
                "Saurzcode API terms of service", "mail2saurzcode@gmail.com",
                "Saurzcode API Licence Type", "Saurzcode API License URL");
        return apiInfo;
    }
    }

并添加到我的一个控制器中:

@ApiOperation(httpMethod = "GET", value = "Say Hello To World using Swagger")
  @RequestMapping(method = RequestMethod.GET)
String get() {
    ...
}

现在,当我使用以下 url 调用我的应用程序时:

http://localhost:8080/api-docs

我得到:

{"apiVersion":"1.0","swaggerVersion":"1.2","info":{"title":"SaurzCode API","description":"API for Saurzcode","termsOfServiceUrl":"Saurzcode API terms of service","contact":"mail2saurzcode@gmail.com","license":"Saurzcode API Licence Type","licenseUrl":"Saurzcode API License URL"}}

从这一点开始,我如何生成正确的 json 文件并让我的 ui 指向它:

在此处输入图像描述

只要我在招摇的用户界面中输入任何网址,我就会得到:

Can't read from server. It may not have the appropriate access-control-origin settings.

我在我的spring客户端中添加了这个来启用cors:

 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        chain.doFilter(req, res);
    }

它解决了,但是:如果我重新启动我的 spring 应用程序,我的浏览器会再次显示此错误。如果我使用隐身模式,它会再次起作用..带有本地缓存​​的东西..有什么想法吗?谢谢你。射线。

4

0 回答 0