Configuration "Jersey + Swagger + Swagger-UI + Maven"

This is the first time I am trying to team up with Jersey and Maven, and I wonder if I will go the right way. I have knitwear, maven and swagger working on my local computer. Soon I want to deploy it in different environments and enable swagger-ui.

  • If I configure the web.xml file to <param-value>http://localhost:8080/api</param-value> , then I see that swagger is working on my local computer. But I will need to change this address every time I want to deploy my code in different environments (for example, going from Dev, QA, to production), and if so, how would I do it or it’s not possible / no, what is the trick for?

  • I want to include swagger-ui in my project. I see online suggestions for manually downloading a file from git and placing it in my project. But I am wondering if there is a maven dependency that I can use instead, so that I can use maven to get the necessary code to use swagger-ui and configure it to work with knitwear. If so, what is the dependency and how to use it to deploy code across multiple environments?

Please provide guidance and links to tutorials, if possible, as I am new to this technology. Also, if I study in my mental process of using jersey / swagger / swagger-ui / maven without manually loading the code from git and being able to deploy the code through several environments, please let me know so that I can look for another way to use REST in my application .

Thank you for your help.

pom.xml:

  <repositories> <repository> <id>maven2-repository.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </repository> </repositories> <properties> <jersey2.version>2.19</jersey2.version> <jaxrs.version>2.0.1</jaxrs.version> </properties> <!-- Dependencies --> <dependencies> <!-- JAX-RS --> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>${jaxrs.version}</version> </dependency> <!-- Jersey 2.19 --> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>${jersey2.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>${jersey2.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>${jersey2.version}</version> </dependency> <!-- Servlet Library --> <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Spring dependencies --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jersey2-jaxrs</artifactId> <version>1.5.0</version> </dependency> </dependencies> 

web.xml

  <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>HelloWorldSpring</display-name> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value> io.swagger.jaxrs.listing, com.jer.rest </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>Jersey2Config</servlet-name> <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class> <init-param> <param-name>api.version</param-name> <param-value>1.0.0</param-value> </init-param> <init-param> <param-name>swagger.api.basepath</param-name> <param-value>http://localhost:8080/HealthTracker/rest</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Other XML Configuration --> <!-- Load by Spring ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <!-- Spring ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> 
+5
source share
3 answers

Here is what I think will answer your questions:

  1. To be able to customize the swagger to suit different environments, follow these steps:

    i) Create a Bootstrap class to configure the Swagger bean (link: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 and Configuring the API ) Version with Swagger UI )

    ii) Set the values ​​in the bean above using the values ​​from the properties file, which you can easily configure outside of your code in any environment.

  2. Swagger dist consists of html / css / image / js files. It cannot be added as a dependency on the jav file of the Maven.

Hope this helps!

+1
source

Check out the link https://github.com/swagger-api/swagger-samples/tree/2.0/java

It has great examples of setting up Swagger 3.0 in your project (see the java-jersey2-webxml example). For earlier versions, check the branches

As for ui, you can download the necessary files, or just add the dependency below to pom.xml.

 <dependency> <groupId>org.webjars</groupId> <artifactId>swagger-ui</artifactId> <version>3.6.1</version> </dependency> 

It will download the necessary ui files. You can copy the downloaded index.html into your project and edit the URL.

+1
source

There is a solution here with javas doclets (no annotations required for Chargs). Use maven-javadoc-plugin and configure swagger-doclet as an alternative doclet . With maven profiles you can manage different environments:

 <profile> <id>dev</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.9.1</version> <executions> <execution> <id>generate-service-docs</id> <phase>generate-resources</phase> <configuration> <doclet>com.carma.swagger.doclet.ServiceDoclet</doclet> <docletArtifact> <groupId>com.carma</groupId> <artifactId>swagger-doclet</artifactId> <version>1.0.3</version> </docletArtifact> <reportOutputDirectory>${project.build.outputDirectory}</reportOutputDirectory> <useStandardDocletOptions>false</useStandardDocletOptions> <additionalparam>-apiVersion 1 -docBasePath https://example.com/apidocs -apiBasePath https://example.com/api -swaggerUiPath ../../../src/main/resources/swagger-ui/ </additionalparam> </configuration> <goals> <goal>javadoc</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> 

I put swagger resources directly in my project. Therefore, it was easy to configure css and html.

swagger-resources

Or, if you do not want to host swagger in your repository, you can use frotend-maven-plugin to control js / css (for example: swagger-ui ) with bower .

I provide swagger as static resources directly from the embedded server (in my case, I used grizzly):

 String apiDocs = Env.getApiDocs(); server.getServerConfiguration().addHttpHandler( new CLStaticHttpHandler(GrizzlyStarter.class.getClassLoader(), apiDocs), apiDocs); 
0
source

Source: https://habr.com/ru/post/1246831/


All Articles