Swagger Relative Basepath

872 views
Skip to first unread message

Jonathan Lemon

unread,
Apr 23, 2014, 8:47:18 AM4/23/14
to swagger-sw...@googlegroups.com
Hi folks,

So I have Swagger running within a JAX-RS implementation.
However, I am having some bother making the base path relative.

At the moment I have it hardcoded to localhost in the java code.
The Swagger UI, I made that relative to get the api-docs.

But, does anyone know how to override the base path for the actual service calls?
Or how to make it relative in the java code? I can get the IP address......just not the port.

Thanks!

Ron

unread,
Apr 27, 2014, 6:51:56 AM4/27/14
to swagger-sw...@googlegroups.com
Generally speaking, you don't want to get it dynamically.

The reason for this is that assuming production code, your server may be behind load balances, proxies, firewalls and whatnot.
If you have a cluster running your application, you'll get a different URL per instance, and that's probably not what you want.

Having it hard-coded is also not the best approach. Assuming you have a build and deploy systems, you'd probably want to rely on those to set the URL based on your environment.
This means either having it as part of your general application configuration, be it in a properties file, properties set by maven (for example) or in your database.


--
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.

Alexander Denissov

unread,
Apr 29, 2014, 1:59:00 PM4/29/14
to swagger-sw...@googlegroups.com
If you need to dynamically figure out the basepath based on the external hostname where your app is deployed, you can write a servlet filter that will figure it out and then set it in the Swagger configuration. 

Inside the filter doFilter() method you can write:

String contextPathUrl = new URL(request.getScheme(), request.getServerName(), request.getServerPort(), filterConfig.getServletContext().getContextPath()).toExternalForm();

ConfigFactory.config().setBasePath(contextPathUrl);

Make sure this is done only once (remember state in a private static boolean) and apply filter mapping in your web.xml

<filter>

    <filter-name>apidoc-config</filter-name>

    <filter-class>my.ApiDocConfigFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>apidoc-config</filter-name>

    <url-pattern>/api-docs/*</url-pattern>

  </filter-mapping>

Shyju Karayi

unread,
Dec 27, 2016, 10:02:23 PM12/27/16
to Swagger, aden...@gopivotal.com
Hi
I have created a class as you mentioned and implemented doFilter method,  but this method is not invoking while server starts.

I have added the below config in web.xml
  <filter>
    <filter-name>apidoc-config</filter-name>
    <filter-class>com.test.servlet.SwaggerConfigFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>apidoc-config</filter-name>
    <url-pattern>/api-docs/*</url-pattern>
  </filter-mapping>

and

calde...@yahoo.com

unread,
Dec 28, 2016, 3:40:46 AM12/28/16
to swagger-sw...@googlegroups.com

--------------------------------------------
On Wed, 12/28/16, Shyju Karayi <shyju...@gmail.com> wrote:

Subject: Re: Swagger Relative Basepath
To: "Swagger" <swagger-sw...@googlegroups.com>
Cc: aden...@gopivotal.com
Date: Wednesday, December 28, 2016, 5:02 AM

HiI
setBasePath(contextPathUrl);Make sure this is done
only once (remember state in a private static boolean) and
apply filter mapping in your
web.xml<filter> 
 
<filter-name>apidoc-config</fi
lter-name>   
<filter-class>my.ApiDocConfigF
ilter</filter-class>  </filter>
 
<filter-mapping> 
 
<filter-name>apidoc-config</fi
lter-name>   
<url-pattern>/api-docs/*</url-
pattern>


















  </filter-mapping>



--

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.
riasul entuziasm starnit de dubla alegere a lui Alexandru I.Cuza pretutindeni unde traiau romani nu putea in nici un fel sa ascunda situatia complexa in care se gasea noul stat.
Reply all
Reply to author
Forward
0 new messages