Search code examples
restswaggerjersey-2.0swagger-uiproperties-file

How to set up jersey and swagger-ui with properties file for setting url variable?


I have a Maven spring application that uses Jersey for a REST API. I have a properties file for each of the environments that I want to deploy the code to.

For example, dev.properties looks like:

hostUrl=https://dev.foo.net/
basePathUrl=/bar/restapi

And test.properties looks like

hostUrl=https://test.foo.net/
basePathUrl=/bar/restapi

I then use spring beans to configure swagger for each environment:

<bean id="beanConfig" class="io.swagger.jaxrs.config.BeanConfig">
    <property name="title" value="Swagger App"/>
    <property name="version" value="1.0.0" />
    <property name="schemes" value="http" />
    <property name="host" value="#{envSpecificProperties.hostUrl}" />
    <property name="basePath" value="#{envSpecificProperties.basePathUrl}"/>
    <property name="resourcePackage" value="com.foo.bar.rest"/>
    <property name="scan" value="true"/>
</bean>

<context:property-placeholder
    properties-ref="envSpecificProperties" />
<util:properties id="envSpecificProperties"
    location="WEB-INF/classes/file-#{xjpEnvironment.domain}.properties" />
<xjp:environment />

My question is, how can I use these same properties files for setting up swagger-ui in index.html so that swagger-ui does not have to be hard coded like this

<script type="text/javascript">
$(function () {     
  var url = window.location.search.match(/url=([^&]+)/);
  if (url && url.length > 1) {
    url = decodeURIComponent(url[1]);
  } else {
    url = "http://localhost:8080/foo/bar/restapi/swagger.json";
  }

And can instead be set dynamically with the properties files in some way like:

<script type="text/javascript">
$(function () {     
  var url = window.location.search.match(/url=([^&]+)/);
  if (url && url.length > 1) {
    url = decodeURIComponent(url[1]);
  } else {
    url = "#{beanConfig.host}" + "#{beanConfig.basePath}" + "swagger.json";
  }

Please let me know if this is possible with maven swagger jersey and spring.

Thanks!


Solution

  • You can add a Bootstrap servlet and set the bean config in there, using values from your properties file.

    For more details refer to: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 and Setting the Api Version with Swagger UI

    To set the values in index.html dynamically, use JavaScript functions to form the url. Something like this:

    url = "http://" + window.location.host + window.location.pathname + "api/swagger.json";