码迷,mamicode.com
首页 > 其他好文 > 详细

用swagger生成接口文档代码

时间:2018-05-24 20:40:15      阅读:557      评论:0      收藏:0      [点我收藏+]

标签:man   enables   eth   bin   void   manager   年龄   int   res   


1、Swagger2类:
package com.example.demo;
import com.google.common.base.Predicate; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.RequestHandler; import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2 { // @ApiOperation(value = "获取指定id用户详细信息", // notes = "根据user的id来获取用户详细信息", // httpMethod = "GET") // @ApiImplicitParams({ // @ApiImplicitParam(name = "userName", value = "用户名", // paramType = "query", required = true, dataType = "String"), // @ApiImplicitParam(name = "password", value = "用户密码", // paramType = "query", required = true, dataType = "String") // }) // @Api //:注解controller,value为@RequestMapping路径 // // @ApiOperation //:注解方法,value为简要描述,notes为全面描述,hidden=true Swagger将不显示该方法,默认为false // // @ApiParam //:注解参数,hidden=true Swagger参数列表将不显示该参数,name对应参数名,value为注释,defaultValue设置默认值,allowableValues设置范围值,required设置参数是否必须,默认为false // // @ApiModel //:注解Model // @ApiModelProperty //:注解Model下的属性,当前端传过来的是一个对象时swagger中该对象的属性注解就是ApiModelProperty中的value @ApiIgnore //:注解类、参数、方法,注解后将不在Swagger UI中显示 @Bean public Docket createRestApi() { Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() { @Override public boolean apply(RequestHandler input) { if (input.isAnnotatedWith(ApiOperation.class)) { return true; } return false; } }; return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // document info .select() .apis(predicate) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("buzhou match") .description("buzhou exchange match system") .license("Apache License Version 2.0") .version("1.0") .build(); } }




2、TestController类:

package com.example.demo;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class TestController {
// private static Logger logger = LogManager.getLogger(TestController.class);


@ApiOperation(value="根路径", httpMethod="GET", notes="这是一个hello world")
@GetMapping("/")
public String getStr() {
// logger.info("****Hello World!****");
System.out.println("****Hello World!****");
return "结果是:返回helloworld";
}




@ApiOperation(value="异常", httpMethod="GET", notes="这是一个除0的异常")
@GetMapping("/zeroException")
public int ZeroException() {
return 100/0;
}




@ApiOperation(value="获取年龄", httpMethod="GET", notes="获取年龄接口")
@GetMapping("/getage")
public int getage(int age) {
return age;
}


private int age;

@ApiOperation(value="设置年龄", httpMethod="GET", notes="设置年龄接口")
@GetMapping("/setage")
public void setAge(int age) {
this.age = age;
}

}

 

 

用swagger生成接口文档代码

标签:man   enables   eth   bin   void   manager   年龄   int   res   

原文地址:https://www.cnblogs.com/lanyy/p/9084796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!