2015-08-20 25 views
10

Sto usando swagger-ui per fornire una buona documentazione per le API REST ai nostri clienti. Internamente abbiamo due diversi ambienti in cui jenkin costruisce il progetto. E.g. swagger.json è accessibile sia su ambiente come: http://www.myhost.com/xyz/rest/swagger.json https://www.myhost2.com/rest/swagger.jsonGestione di più basepath in swagger

La documentazione è disponibile come: http://www.myhost.com/xyz/dist/index.html https://www.myhost2.com/dist/index.html

spavalderia api BasePath in web.xml è:

<init-param>  
    <param-name>swagger.api.basepath</param-name> 
    <param-value>/rest</param-value> 
</init-param> 

PROBLEMA: sto cercando di utilizzare la funzione "Provalo" nella pagina di documentazione. La relativa richiesta URL per entrambi i padroni di casa sono i seguenti: http://www.myhost.com/rest/getAUser https://www.myhost2.com/rest/getAUser

Si lavora per host2 quanto sta colpendo l'URL corretto. Tuttavia dovrebbe colpire http://www.myhost.com/xyz/rest/getAUser per host1 ma sta colpendo l'url http://www.myhost.com/rest/getAUser.

Esiste un modo per specificare più percorsi per diversi URL.

Il mio swagger-ui html è simile a questo.

$(function() { 
var href = window.location.href; 
var url = href.substring(0, href.lastIndexOf("/dist")); 
console.log(url); 
// Pre load translate... 
if(window.SwaggerTranslator) { 
window.SwaggerTranslator.translate(); 
} 
window.swaggerUi = new SwaggerUi({ 
url: url + "/rest/swagger.json", 
dom_id: "swagger-ui-container", 
...... 
...... 
} 
+1

Completamente irrilevante alla domanda, ma '/ getAUser' non è, ovviamente, design REST corretto. GET è l'operazione e il tuo url dovrebbe consistere di nomi. – Jasper

risposta

6

sono stato in grado di risolvere questo problema configurando spavalderia utilizzando BeanConfig invece di utilizzare Servlet in web.xml

BeanConfig classe:

public class SwaggerBootstrap extends DefaultJaxrsConfig { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = myAutoGeneratedID; 

    @Override 
    public void init(ServletConfig config) throws ServletException { 

     super.init(config); 
     //contextPath will be null for host2 and /xyz for host1. 
     String contextPath = config.getServletContext().getContextPath(); 

     BeanConfig beanConfig = new BeanConfig(); 
     beanConfig.setVersion("1.0.0"); 
     beanConfig.setTitle("My API Documentation"); 
     beanConfig.setSchemes(new String[] { 
       "http", "https" 
     }); 
     beanConfig 
     .setResourcePackage("com.example.my.rest.api.package"); 

     beanConfig.setBasePath(contextPath + "/rest"); 
     beanConfig.setScan(true); 
    } 
} 

e in web.xml:

<servlet> 
     <servlet-name>SwaggerBootstrap</servlet-name> 
     <servlet-class>my.package.to.SwaggerBootstrap</servlet-class> 
     <init-param> 
      <!-- This make sure that all resources are scanned whether or not they use Swagger Annotations. 
      https://github.com/swagger-api/swagger-samples/tree/master/java/java-jaxrs-no-annotations --> 
      <param-name>scan.all.resources</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 

E ho cambiato il mio pom.xml per iniziare a utilizzare l'ultima versione stabile di swagger-jersey2-jaxrs:

<dependency> 
      <groupId>io.swagger</groupId> 
      <artifactId>swagger-jersey2-jaxrs</artifactId> 
      <version>1.5.3</version> 
     </dependency>