swagger生成接口文档php,用swagger生成接口文档代码

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 predicate = new Predicate() {

@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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值