New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question - Swagger UI and Swagger Services on different Servers #1001

Closed
HashtagMarkus opened this Issue Oct 9, 2015 · 5 comments

Comments

Projects
None yet
4 participants
@HashtagMarkus

HashtagMarkus commented Oct 9, 2015

Hi,
I am currently setting up a small microservice architecture. I would like to run the Swagger UI in a docker container and my rest services in another.
I can't find a way to make my services known to the swagger ui. I am using the swagger-ui maven artifact which I included in a Springboot application.

e.g:
Swagger UI Docker Container: http://localhost:8080
RestService1 Docker Container: http://localhost:8081
RestService2 Docker Container: http://localhost:8082

I can access the /api-docs path on both services. However I can't figure out how to configure Swagger UI accordingly.
How can I configure Swagger UI to discover the api-docs endpoints of my other docker container?
Unfortunately I can't find any resources addressing this setup. Is this even possible using the maven archetype?

@mozinrat

This comment has been minimized.

mozinrat commented Oct 9, 2015

We are looking for exact same question, and what would be best if we can use some discovery mechanism to automatically add those apidocs as dropdown, or may be some way an application can push the api-docs for registering the docs.

@HashtagMarkus

This comment has been minimized.

HashtagMarkus commented Oct 13, 2015

I probably found a solution by reading through old issues:
http://grokbase.com/t/gg/swagger-swaggersocket/1579hd7hzy/swagger-ui-from-multiple-microservices-springmvc/157e1j8xqn#157e1j8xqn

I created my own ApiResourceController (Basically I copied the one from springfox and removed all the DocumentationCache stuff). I then changed the swaggerResource() method to return my services:

@RequestMapping(value = "/swagger-resources")
@ResponseBody
ResponseEntity<List<SwaggerResource>> swaggerResources() {

    List<SwaggerResource> resources = new ArrayList<SwaggerResource>();

    SwaggerResource swaggerResource = resource("myservice", "http://localhost:8081/myservice/v0");
    swaggerResource.setSwaggerVersion("2.0");
    resources.add(swaggerResource);

    SwaggerResource swaggerResource2 = resource("myservice2", "http://localhost:8082/myservice2/v0");
    swaggerResource2.setSwaggerVersion("2.0");
    resources.add(swaggerResource2);

    Collections.sort(resources);
    return new ResponseEntity<List<SwaggerResource>>(resources, HttpStatus.OK);
}

Obviously this is not the best solution, because I don't want to hardcode my resources. I guess I'll write some kind of registry which is called by each service on startup.

Since spring will complain about duplicated resource mappings I had to ignore the existing ApiResourceController by adding the excludeFilters to my ComponentScan annotation:
@ComponentScan(excludeFilters = @ComponentScan.Filter(value = springfox.documentation.swagger.web.ApiResourceController.class, type = FilterType.ASSIGNABLE_TYPE))

Another thing that needs to be done, is to enable CORS, which can be done by writing an own CORS Filter as described here: https://spring.io/guides/gs/rest-service-cors/

This is however not working in my current application. So If someone has an idea what might go wrong here, please let me know 👍
I receive the following exception Can't read from server. It may not have the appropriate access-control-origin settings.. When I curl my resources I receive the following header:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
X-Application-Context: application:8081
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked

which looks fine for me. I have no clue why this is not working

@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Oct 13, 2015

@HashtagMarkus I would've suggested suggested the same thing. The CORS filter setup is pretty simple to setup. The only catch is like you said, you'll be hard coding the services without access to a registry.

@dilipkrish dilipkrish added the question label Oct 15, 2015

@dilipkrish dilipkrish added this to the 2.3.0 milestone Oct 15, 2015

@seshachalam

This comment has been minimized.

seshachalam commented Jul 18, 2017

Hi @HashtagMarkus & @dilipkrish,

I am having the similar kind of requirement. I have created ApiResourceController in my local. But i am not able to exclude springfox.documentation.swagger.web.ApiResourceController.class, using the below.

@componentscan(excludeFilters = @componentscan.Filter(value = springfox.documentation.swagger.web.ApiResourceController.class, type = FilterType.ASSIGNABLE_TYPE))

ITs still failing with the below error.

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'apiResourceController' for bean class [springfox.documentation.swagger.web.ApiResourceController] conflicts with existing, non-compatible bean definition of same name and class [************.local..ApiResourceController]

Any suggestions?

@dilipkrish

This comment has been minimized.

Member

dilipkrish commented Jul 21, 2017

@seshachalam Annotate yours with @Primary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment