未为 Spring 启动应用程序生成 Swagger 文档
Swagger Docs not generating for Spring Boot app
我是 spring 引导的新手,但出于某种原因,我的招摇 ui 不允许我访问 API。我尝试按照几个教程进行操作,但没有看到 API 的运气。下面是我在加载时得到的屏幕截图(我也尝试了其他几个 URL)和一些相关代码。
Spring 引导应用程序
@SpringBootApplication(scanBasePackages={"com.starter.controllers"})
public class StarterRestStarterApplication {
public static void main(String[] args) {
SpringApplication.run(StarterRestStarterApplication.class, args);
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("A simple starter service")
.description("A simple calculator REST service made with Spring Boot in Java")
.contact(new Contact("my info", "http://myurl.com", "myemail@gmail.com"))
.version("1.0")
.build();
}
}
问候控制器
@RestController
@RequestMapping("api/v1")
public class GreetingController {
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new GreetingService().greet(name);
}
}
Gradle.build
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
group = 'com.starter'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
ext {
springCloudVersion = 'Dalston.SR4'
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
compile("org.springframework.boot:spring-boot-starter-web")
compile 'io.springfox:springfox-swagger-ui:2.7.0'
compile "io.springfox:springfox-swagger2:2.7.0"
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
将 @EnableSwagger2
添加到您的配置 class。
我是 spring 引导的新手,但出于某种原因,我的招摇 ui 不允许我访问 API。我尝试按照几个教程进行操作,但没有看到 API 的运气。下面是我在加载时得到的屏幕截图(我也尝试了其他几个 URL)和一些相关代码。
Spring 引导应用程序
@SpringBootApplication(scanBasePackages={"com.starter.controllers"})
public class StarterRestStarterApplication {
public static void main(String[] args) {
SpringApplication.run(StarterRestStarterApplication.class, args);
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("A simple starter service")
.description("A simple calculator REST service made with Spring Boot in Java")
.contact(new Contact("my info", "http://myurl.com", "myemail@gmail.com"))
.version("1.0")
.build();
}
}
问候控制器
@RestController
@RequestMapping("api/v1")
public class GreetingController {
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new GreetingService().greet(name);
}
}
Gradle.build
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
group = 'com.starter'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
ext {
springCloudVersion = 'Dalston.SR4'
}
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
compile("org.springframework.boot:spring-boot-starter-web")
compile 'io.springfox:springfox-swagger-ui:2.7.0'
compile "io.springfox:springfox-swagger2:2.7.0"
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
将 @EnableSwagger2
添加到您的配置 class。