當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JAVA入门[23]-SpringBoot配置Swagger2
生活随笔
收集整理的這篇文章主要介紹了
JAVA入门[23]-SpringBoot配置Swagger2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、新建SpringBoot站點
1.新建module,然后引入pom依賴:
?
2.新建Controller文件
@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啟動文件
@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=aa
二、配置Swagger2
1.引入Swagger pom依賴項
?
2.添加Swagger2 java配置文件
@Configuration @EnableSwagger2 public class Swagger2Config {@Beanpublic 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注解
@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
?
總結
以上是生活随笔為你收集整理的JAVA入门[23]-SpringBoot配置Swagger2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 异步FIFO的FPGA实现
- 下一篇: 告别2017!2018投影市场变革机遇与