1、新建SpringBoot站點
1.新建module,而後引入pom依賴:html
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2.新建Controller文件java
@RestController @RequestMapping("/demo") public class DemoController { @RequestMapping(value = "/index",method= RequestMethod.GET) public String index(@RequestParam(value="name", required=false, defaultValue="World") String name) { return "demo "+name; } }
3.新建SpringBoot啓動文件web
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class ContentPlatformWebapiApplication { public static void main(String[] args) { SpringApplication.run(ContentPlatformWebapiApplication.class,args); } }
4.運行,http://localhost:8080/demo/index?name=aaspring
2、配置Swagger2
1.引入Swagger pom依賴項api
<dependencies> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency> </dependencies>
2.添加Swagger2 java配置文件app
@Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage(「com.cathy.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("測試接口平臺API") .description(「cathy demo API.") .termsOfServiceUrl("Terms of service") .contact("myeaddress@company.com") .version("1.0") .build(); } }
3.爲controller添加swagger註解spring-boot
@RestController @RequestMapping("/demo") @Api(value = "API - DemoController", description = "demo接口") public class DemoController { @RequestMapping(value = "/index", method = RequestMethod.GET) @ApiOperation(value = "首頁", notes = "demo index") @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "name", required = false, dataType = "string", paramType = "query", defaultValue = "World") }) public String index(@RequestParam(value = "name", required = false, defaultValue = "World") String name) { return "demo " + name; } }
4.運行:http://localhost:8080/swagger-ui.html測試