我将Swagger-UI安装为由nginx服务器提供服务的独立用户。
我们有几个微服务,我正试图从我们的spring-boot应用程序中实现swagger json。
因此,如果我理解正确的话,我需要将swagger-ui指向我的应用程序的swagger端点,以便接收该json信息。
我是怎么做到的?
on gradle:
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() {
...
}
现在,当我使用以下网址调用我的应用时:
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文件并实际让我的swagger ui指向它:
一旦我输入任何url进入swagger ui,我得到:
Can't read from server. It may not have the appropriate access-control-origin settings.
我在我的春天客户端添加了这个以启用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);
}
它解决但是: 如果我重启我的春季应用程序,我的浏览器再次显示此错误 如果我使用隐身模式,它再次工作..具有本地缓存的东西..任何想法? 谢谢。 射线。