How to add swagger for a existing Spring rest service ? using the spingfox or swagger UI
This is using a Spring Boot application
To start with create a normal REST API using spring,
1.
In your pom.xml make sure you have all the swagger the dependencies in place.
<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
Do not forget @EnableSwagger2
And please remember to add the bean with the docket method else it will add the junk api values
3) In your rest controller
Do not miss the api annotations this would be needed in the swagger editor for proper handling
Which would be later in the swagger UI.
After doing all of this if you run your spring boot app and
that is very much it you should get the api documentation
Like
For Swagger UI
In case you get a error like in the 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,
First add a CORS plugin for chrome/any browser - add your url under this like
Then this would start your swagger UI , and you must be able to see the output on the browser .
If you are using jackson then make sure you use proper version numbers
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 .
Make sure you have the pom correctly .
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.
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. It looks like it was added in 2.1.0.
So, you'll need to fix the dependencies - most likely by excluding 2.0.5 and including 2.1.0.
Secondly,
If you have to consume this Swagger generated codegen or swagger codegen
First, you need to add maven dependencies for Swagger in pom.xml
<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:
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. If you have some questions, you can refer to sample repository with the simular project https://github.com/mv200580/springboot-rest .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.