JAVA Swagger2 教程 demo

import io.swagger.annotations.*;
import me.ele.ecs.project.collection.util.Response;
import me.ele.ecs.project.collection.util.ResponseUtil;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Api(value = "TestController", description = "測試controller")
@RestController
public class SwaggerDemoController {

    @ApiOperation(value = "測試", notes = "測試接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "name", value = "姓名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "path", name = "age", value = "年齡", required = false, dataType = "Integer")
    })
    @GetMapping("/test")
    public Response test(@RequestParam("name") String name, @RequestParam(value = "age", required = false) Integer age){
        TestD testD1 = new TestD();
        testD1.setName(name);
        testD1.setAge(age);
        return ResponseUtil.success(testD1);
    }

    @ApiOperation(value = "測試2", notes = "測試接口2", httpMethod = "POST")
    @PostMapping("/test2")
    public Response test2(@RequestBody @Valid @ApiParam(name = "用戶對象", value = "json格式", required = true) TestD testD){

        return ResponseUtil.success(testD);
    }


}

@ApiModel(value = "測試對象", description = "用來測試的對象")
class TestD {
    @ApiModelProperty(value = "用戶名", name = "name", example = "zhangsan")
    @NotNull
    private String name;

    @ApiModelProperty(value = "年齡", name = "age", example = "1")
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
相關文章
相關標籤/搜索