简体   繁体   English

Swagger 2.0 在 spring MVC rest api 中使用 spring boot 实现

[英]Swagger 2.0 Implementation in spring MVC rest api with spring boot

How to add swagger for a existing Spring rest service ?如何为现有的 Spring 休息服务添加 swagger? using the spingfox or swagger UI使用 spingfox 或 swagger UI

This is using a Spring Boot application这是使用 Spring Boot 应用程序

To start with create a normal REST API using spring,首先使用 spring 创建一个普通的 REST API,

1. 1.

In your pom.xml make sure you have all the swagger the dependencies in place.在你的 pom.xml 中,确保你有所有的 swagger 依赖项。

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.2.2</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.2.2</version>

<scope>compile</scope>

</dependency>

2) In your spring boot application 2)在你的spring boot应用程序中在此处输入图片说明

Do not forget @EnableSwagger2不要忘记@EnableSwagger2

And please remember to add the bean with the docket method else it will add the junk api values请记住使用 docket 方法添加 bean,否则它会添加垃圾 api 值

3) In your rest controller 3)在你的休息控制器中

在此处输入图片说明 Do not miss the api annotations this would be needed in the swagger editor for proper handling不要错过在 swagger 编辑器中进行正确处理所需的 api 注释

Which would be later in the swagger UI.稍后将出现在 swagger UI 中。

在此处输入图片说明

After doing all of this if you run your spring boot app and在完成所有这些之后,如果你运行你的 spring boot 应用程序并且

that is very much it you should get the api documentation就是这样,您应该获得 api 文档

Like喜欢

在此处输入图片说明

For Swagger UI对于 Swagger UI

In case you get a error like in the swagger UI如果您在 swagger UI 中遇到错误

在此处输入图片说明

Can't read from server.无法从服务器读取。 It may not have the appropriate access-control-origin settings.它可能没有适当的访问控制来源设置。

To resolve this issue, you need to access-control-origin settings for swagger,要解决此问题,您需要对 swagger 进行 access-control-origin 设置,

First add a CORS plugin for chrome/any browser - add your url under this like首先为 chrome/任何浏览器添加一个 CORS 插件 - 在这个下面添加你的 url

在此处输入图片说明

Then this would start your swagger UI , and you must be able to see the output on the browser .然后这将启动您的 swagger UI,并且您必须能够在浏览器上看到输出。

在此处输入图片说明

If you are using jackson then make sure you use proper version numbers如果您使用的是 jackson,请确保使用正确的版本号

java.lang.NoSuchMethodError: com.fasterxml.jackson. java.lang.NoSuchMethodError: com.fasterxml.jackson。

Or any other Jackson related errors are caused due to the version mismatch between the Jackson core and Jackson databind dependency in the pom.xml .或者任何其他与 Jackson 相关的错误都是由于 pom.xml 中的 Jackson 核心和 Jackson 数据绑定依赖项之间的版本不匹配而引起的。

Make sure you have the pom correctly .确保你有正确的 pom。

In my case,就我而言,

the problem was that I was getting incompatible versions of jackson-core and jackson-databind - jackson-core 2.0.5 is being pulled in, but I believe at least 2.1.0 is required.问题是我得到了不兼容的 jackson-core 和 jackson-databind 版本 - jackson-core 2.0.5 正在被引入,但我相信至少需要 2.1.0。

The first line of the exception tells you that it can't find the method JsonParser.getValueAsString(), looking at the API docs for 2.0.5, that method indeed does not exist.异常的第一行告诉你它找不到 JsonParser.getValueAsString() 方法,查看 2.0.5 的 API 文档,该方法确实不存在。 It looks like it was added in 2.1.0.看起来它是在 2.1.0 中添加的。

So, you'll need to fix the dependencies - most likely by excluding 2.0.5 and including 2.1.0.因此,您需要修复依赖项 - 最有可能通过排除 2.0.5 并包括 2.1.0。

Secondly,其次,

If you have to consume this Swagger generated codegen or swagger codegen如果你必须使用这个 Swagger 生成的 codegen 或 swagger codegen

First, you need to add maven dependencies for Swagger in pom.xml首先需要在pom.xml中为Swagger添加maven依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.4.0</version>
</dependency>

then add SwaggerConfiguration class:然后添加 SwaggerConfiguration 类:

package com.mycompany.rest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by smv on 10.09.2016.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

That's all you need for minimal Swagger UI configuration for your springboot project.这就是您的 springboot 项目的最小 Swagger UI 配置所需的全部内容。 If you have some questions, you can refer to sample repository with the simular project https://github.com/mv200580/springboot-rest .如果您有任何问题,可以参考带有模拟项目https://github.com/mv200580/springboot-rest 的示例存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM