http://heidloff.net/article/usage-of-swagger-2-0-in-spring-boot-applications-to-document-apis/
- Swagger
- springfox-swagger
- Spring-boot
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RELEASE")
}
}
apply plugin: 'spring-boot'
repositories {
mavenCentral()
mavenLocal()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.4.0'
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.4.0'
compile group: 'io.springfox', name: 'springfox-spi', version: '2.4.0'
}
Application.java 微服务程序入口
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@ComponentScan("hello")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public Docket newsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("greetings")
.apiInfo(apiInfo())
.select()
.paths(PathSelectors.regex("/greeting.*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Rest Sample With Swagger Title")
.description("Spring Rest sampele with Swagger Description")
.termsOfServiceUrl("")
.contact(new Contact("king.wang", "wdxxl.github.io", "[email protected]"))
.licenseUrl("")
.version("1.0")
.build();
}
}
Greetingcontroller.java
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
@RestController
public class Greetingcontroller {
private static final String TEMPLATE = "Hello, %s !";
private final AtomicLong counter = new AtomicLong();
@ApiOperation(value = "getGreeting", nickname = "getGreeting")
@RequestMapping(method = RequestMethod.GET, path = "/greeting", produces = "application/json")
@ApiImplicitParams({@ApiImplicitParam(name = "name", value = "User's name", required = false,
dataType = "string", paramType = "query", defaultValue = "Niklas")})
@ApiResponses(value = {@ApiResponse(code = 200, message = "Success", response = Greeting.class),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure")})
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(TEMPLATE, name));
}
}
Greeting.java
package hello;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
@JsonProperty(required = true)
@ApiModelProperty(notes = "The name of the user", required = true)
public String getContent() {
return content;
}
}
README.md 相关的访问网址信息
http://localhost:8080/greeting?name=king
http://localhost:8080/greeting
http://localhost:8080/v2/api-docs?group=greetings
http://localhost:8080/swagger-ui.html