Swagger UI from multiple microservices SpringMVC

2,259 views
Skip to first unread message

sami...@gmail.com

unread,
Jul 9, 2015, 1:37:58 PM7/9/15
to swagger-sw...@googlegroups.com
Hi, 

I have set up swagger and swagger ui for several different micro-services. At this point I would like to be able to serve up one swagger ui page that draws from each service.  

I believe someone had  a similar question but i don't understand the solution.

Can I have some help on how I would go about creating one swagger ui page that draws from these different micro-services?

I am using swagger-springmvc version 1.0.2 and swagger-spring-mvc-ui version 0.4.

Thanks!

Ron Ratovsky

unread,
Jul 9, 2015, 2:46:38 PM7/9/15
to swagger-sw...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------
Message has been deleted

Ron Ratovsky

unread,
Jul 9, 2015, 3:01:10 PM7/9/15
to swagger-sw...@googlegroups.com
It shows how to load two SwaggerUi objects, each can point to a different URL loading a different microservice.
If you need to add more than that, you can base it off on that.

On Thu, Jul 9, 2015 at 9:59 PM, <sami...@gmail.com> wrote:
Hi Ron,

Thanks for you response!
I don't fully understand what this html page does. I'm not very familiar with javascript or swagger.
Can you give me an explanation for what this does?

sami...@gmail.com

unread,
Jul 9, 2015, 3:01:58 PM7/9/15
to swagger-sw...@googlegroups.com
Hi Ron,

Thanks for your response.
Unfortunately I don't really understand what this html page does. I'm not very familiar with javascript.
Can you give me a bit of an explanation?
Thanks

On Thursday, July 9, 2015 at 11:46:38 AM UTC-7, Ron wrote:

Ron Ratovsky

unread,
Jul 9, 2015, 3:04:02 PM7/9/15
to swagger-sw...@googlegroups.com
It's a replacement to the index.html from the swagger-ui project. Start by downloading it and playing around with it. If you want to manipulate it, you need to start learning it.

Dilip Krishnan

unread,
Jul 10, 2015, 12:35:27 AM7/10/15
to swagger-sw...@googlegroups.com
Its probably fairly simple to do that using the springfox library without changing much of the ui code or messing with html etc.

Here are the steps for a spring specific solution: 

- Create your new spring boot app
- Create a controller similar to this one ApiResourceController.java
   - Create request mappings needed by the swagger-ui (/swagger-resources, /configuration/security, /configuration/ui)
   - provide a list of SwaggerResources that point to the different micro services you have. Doesn't matter how you load it as long as you have a list of swagger resources that the ui can use.

This solution however is not limited to  spring based solutions, as long as you include the web jar and create resources at those urls and adhere to the json contract, it should help you bootstrap any application with an uber swagger-ui; be it jax-rs, jersey, drop wizard etc.

This is actually Adrian's, idea (awesome, I might add) which I believe he brought up in this forum a while back. We've been using it and it works like a charm. 

If you have trouble with that let me know or post issues @ springfox.io

-Dilip

sami...@gmail.com

unread,
Jul 10, 2015, 6:42:28 PM7/10/15
to swagger-sw...@googlegroups.com
Hi Dilip,

I've created a spring boot application that has the ApiResourceController. This new application uses springfox-swagger-ui version 2 and springfox-swagger 2
I have two questions
How would I provide a list of SwaggerResources to this class?
and 
How should I configure these SwaggerResource objects. What should their location be set to?

Thanks!

sami...@gmail.com

unread,
Jul 10, 2015, 6:50:46 PM7/10/15
to swagger-sw...@googlegroups.com
I think I would somehow need to put SwaggerResource objects into the documentationCache, but I'm not sure how to do this.

Dilip Krishnan

unread,
Jul 10, 2015, 10:12:31 PM7/10/15
to swagger-sw...@googlegroups.com
I dint mean copy it literally, If you know the locations of your services, you could simply add it manually, or from a configuration file or load it from a database. 


@RequestMapping(value = "/swagger-resources")
@ResponseBody
ResponseEntity<List<SwaggerResource>> swaggerResources() {
List<SwaggerResource> resources = new ArrayList<SwaggerResource>();
resources.add(swaggerResource("order-service", "http://example.org/orders", "1.2"));
resources.add(swaggerResource("customer-service", "http://example.org/customers", "1.2"));

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

private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setLocation(location);
swaggerResource.setName(name);
swaggerResource.setName(version);
return swaggerResource;
}

sami...@gmail.com

unread,
Jul 13, 2015, 8:59:21 PM7/13/15
to swagger-sw...@googlegroups.com
Thanks a ton Dilip!
Following you guidance my code pretty much works.
However, I've noticed that SwaggerResources provide a relative path, relative to a base path.
Since I want to pull API's from multiple micro-services on different servers, I'm wondering is there a way to have to access swagger jsons on other pages, perhaps by forcing SwaggerResources to provide absolute paths?

I've posted this question on github as well https://github.com/springfox/springfox/issues/843

Thanks again!

Dilip Krishnan

unread,
Jul 13, 2015, 9:33:02 PM7/13/15
to swagger-sw...@googlegroups.com
Ah don't think of that. Will get that feature request going :)

sami...@gmail.com

unread,
Jul 20, 2015, 8:03:38 PM7/20/15
to swagger-sw...@googlegroups.com
Big Thanks to Ron & Dillip for all their help.

The feature has been incorporated in the latest release of springfox 2.1.1
Reply all
Reply to author
Forward
0 new messages