회사에서 spring boot와 open-api swagger를 사용하고 있다.
이번에 새로운 프로젝트에 1.6.8 버전의 open-api를 추가했는데 api 실행 시 아래와 같은 오류가 발생했다.
로컬에서는 정상 작동하는데 aws에 올린 서버에서 문제가 되었다. (local - http / aws 서버 - https)
Failed to fetch.
Possible Reasons:
CORS
Network Failure
URL scheme must be "http" or "https" for CORS request.
swagger cors 오류
확인해보니 요청 URL이 http 다. 내가 실행한서버는 https인데
아래를 보면 서버 url과 swagger 요청 url 이 다른걸 확인할 수 있다
swagger 요청 url이 https가 아닌 http로 나옴
SwaggerConfig.java설정 파일에서
@Bean
public OpenAPI openAPI() 메소드의 설정을 변경해주었다.
요청해야하는 URL을 직접 아래와 같이 설정해주었다.
Server객체를 만들어서 url을 설정해주고 addServersItem으로 추가해주었더니 오류가 해결되었다.
@Bean
public OpenAPI openAPI() {
String jwt = "JWT";
SecurityRequirement securityRequirement = new SecurityRequirement().addList(jwt);
Components components = new Components().addSecuritySchemes(jwt, new SecurityScheme()
.name(jwt)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
);
Server server = new Server();
server.setUrl("https://요청하는URL로 바꿔주세요");
return new OpenAPI()
.components(new Components())
.info(apiInfo())
.addSecurityItem(securityRequirement)
.components(components)
.addServersItem(server);
}
[참고한 글]
https://github.com/springdoc/springdoc-openapi/issues/726
https://github.com/springdoc/springdoc-openapi/issues/118
https://github.com/springdoc/springdoc-openapi/issues/769