eclipse Jersey + Swagger + Swagger-UI + Maven 配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36536272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey + Swagger + Swagger-UI + Maven configuration
提问by
I am trying swagger for the first time with Jersey and Maven, and I am wondering if I am going down the right path. I have jersey, maven, and swagger working on my local computer. Soon, I want to deploy it to different environments and include swagger-ui.
我第一次尝试使用 Jersey 和 Maven 大摇大摆,我想知道我是否走上了正确的道路。我有 jersey、maven 和 swagger 在我的本地计算机上工作。很快,我想将它部署到不同的环境并包含 swagger-ui。
If I configure my web.xml file to
<param-value>http://localhost:8080/api</param-value>
then I see that swagger works on my local computer. But will I need to change this address every time I want to deploy my code to different environments (for example going from a Dev environment, to QA environment, to Production environment), and if so how would I go about doing that or is it not possible/not what swagger is meant for?I want to incorporate swagger-ui with my project. I see online suggestions of downloading the file manually from git and placing it in my project. But what I am wondering is 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 jersey. If so what is the dependency and how do I use it to deploy the code through multiple environments?
如果我将我的 web.xml 文件配置为
<param-value>http://localhost:8080/api</param-value>
然后我看到 swagger 在我的本地计算机上工作。但是每次我想将我的代码部署到不同的环境(例如从开发环境到 QA 环境到生产环境)时,我是否需要更改此地址,如果是这样,我将如何去做,或者是这样吗?不可能/不是什么大摇大摆的意思?我想将 swagger-ui 与我的项目结合起来。我看到在线建议从 git 手动下载文件并将其放入我的项目中。但我想知道的是,是否有一个 maven 依赖项可供我使用,以便我可以使用 maven 获取必要的代码来使用 swagger-ui 并将其配置为与 jersey 一起使用。如果是这样,依赖关系是什么,我如何使用它通过多个环境部署代码?
Please give guidance and links to tutorials if possible seeing as I am new to this technology. Also if I am way off in my thought process of using jersey/swagger/swagger-ui/maven without manually downloading code from git and being able to deploy the code through multiple environments please let me know so I can look for another way to use REST in my application.
如果可能,请提供指导和教程链接,因为我是这项技术的新手。此外,如果我在使用 jersey/swagger/swagger-ui/maven 而没有从 git 手动下载代码并且能够通过多个环境部署代码的思考过程中走得很远,请告诉我,以便我可以寻找另一种使用方式在我的应用程序中休息。
Thank you for your help.
感谢您的帮助。
pom.xml:
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
<?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>
采纳答案by Sampada
Here's what I think would answer your questions:
以下是我认为可以回答您的问题:
To be able to configure swagger as per different environments, then these are the steps you can follow:
i) Create a Bootstrap class to configure swagger bean (ref: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5and Setting the Api Version with Swagger UI)
ii) Set the values in the above bean using values from a properties file, which you can easily configure outside your code in any environment.
Swagger dist consists of html/css/image/js files. It cannot be added as a Maven jar dependency.
为了能够根据不同的环境配置 swagger,您可以遵循以下步骤:
i) 创建一个 Bootstrap 类来配置 swagger bean(参考:https: //github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5and Setting the Api带有 Swagger UI 的版本)
ii) 使用属性文件中的值设置上述 bean 中的值,您可以在任何环境中的代码之外轻松配置该值。
Swagger dist 由 html/css/image/js 文件组成。它不能作为 Maven jar 依赖项添加。
Hope this helps!
希望这可以帮助!
回答by Mopidevi Raghava
Check out the link https://github.com/swagger-api/swagger-samples/tree/2.0/java
查看链接https://github.com/swagger-api/swagger-samples/tree/2.0/java
It has excellent examples on how to configure Swagger 3.0 in your project (see java-jersey2-webxml example). For earlier versions check the branches
它有关于如何在您的项目中配置 Swagger 3.0 的优秀示例(请参阅 java-jersey2-webxml 示例)。对于早期版本检查分支
As far as the ui is concerned, you can download required files or you can just add the below dependency in pom.xml.
就ui而言,您可以下载所需的文件,也可以在pom.xml中添加以下依赖项。
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.6.1</version>
</dependency>
It will download necessary ui files. You can copy the downloaded index.html in your project and edit the url.
它将下载必要的 ui 文件。您可以将下载的 index.html 复制到您的项目中并编辑 url。
回答by Meiko Rachimow
Here a solution with java doclets (no swagger annotations are required). Use the maven-javadoc-pluginand configure the swagger-docletas an alternate doclet. With maven profilesit is possible to manage different environments:
这是一个带有 java doclets 的解决方案(不需要大摇大摆的注释)。使用maven-javadoc-plugin并将swagger-doclet配置为备用 doclet。使用maven 配置文件可以管理不同的环境:
<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 the swagger resources directly into my project. So it was easy to customize the css and html.
我把招摇的资源直接放到了我的项目中。所以自定义 css 和 html 很容易。
Or, if you do not want to put swagger into your repository, you could use the frotend-maven-pluginto manage your js/css dependencies (like: swagger-ui) with bower.
或者,如果您不想将 swagger 放入您的存储库,您可以使用frotend-maven-plugin来管理您的 js/css 依赖项(例如:swagger-ui)和bower。
I deliver swagger as static resources from the embedded server directly (in my case I used grizzly) :
我直接从嵌入式服务器提供 swagger 作为静态资源(在我的例子中我使用了 grizzly):
String apiDocs = Env.getApiDocs();
server.getServerConfiguration().addHttpHandler(
new CLStaticHttpHandler(GrizzlyStarter.class.getClassLoader(), apiDocs), apiDocs);
回答by Aleksey
I was faced with the same problem and created a library that if included into a Jersey project will add swagger 3.0 UI. Please take a look codeand blog postThe idea is that all swagger UI static content is packaged withing the library and extracted at runtime. In addition, library will take care of creating web context for swagger UI and adjust reference to the openapi.json
file. You will need to add 2 properties and this code to your project:
我遇到了同样的问题并创建了一个库,如果包含在 Jersey 项目中,它将添加 swagger 3.0 UI。请看一下代码和博客文章想法是所有 swagger UI 静态内容都与库一起打包并在运行时提取。此外,库将负责为 swagger UI 创建 Web 上下文并调整对openapi.json
文件的引用。您需要将 2 个属性和此代码添加到您的项目中:
SwaggerContext.addSwaggerServlet(tomcat, context,
ConfigBuilder.builder(ConfigType.TYPE_SAFE)
.build()
.getConfig("swagger"),
EmailApplication.class);
and properties:
和属性:
swagger.package="com.itzap"
swagger.apiBaseUrl="http://{application url}"
If all other configuration is left at defaults swagger UI can be accessed over this URL:
如果所有其他配置保留默认值,则可以通过此 URL 访问 swagger UI:
http://{application base URL}/api/v1/swagger