【发布时间】:2018-04-11 00:31:32
【问题描述】:
我对 Spring Boot 很陌生,但由于某种原因,我的招摇用户界面不允许我访问 API。我尝试遵循几个教程,但没有看到 API 的运气。下面是加载时得到的屏幕截图(我也尝试了其他几个 URL)和一些相关代码。
Spring Boot 应用程序
@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}"
}
}
【问题讨论】:
-
你可以尝试将@RequestMapping("api/v1") 替换为@RequestMapping(value="api/v1") 和@RequestMapping("/greeting") 替换为@RequestMapping(value = "/问候")
-
尝试将@EnableSwagger2 添加到您的配置类中。
-
谢谢@Justas,你是 100% 正确的。我不小心忘记了包括在内。谢谢
标签: rest spring-boot swagger swagger-ui