swagger接口文档

Obatis开源项目集成了swagger文档功能,通过注解的形式让后端API接口直接以HTML的形式输出,无需产出额外的接口文档,减少后端开发人员的工作量。配置如下:

package com.Obatis.test.web;

import com.Obatis.test.service.ITestService;
import com.Obatis.test.web.command.TestInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "【测试模块】-展示接口访问和注解配置")
@RestController
@RequestMapping("test")
public class TestController {

@Resource
private ITestService testService;

@PostMapping(value = "add")
@ApiOperation(value = "添加", produces = "application/json")
public void add(@RequestBody @Valid TestInfo info) {
this.testService.add(info);
}
}
package com.Obatis.test.web.command;

import com.Obatis.core.annotation.validator.IsPhoneNumber;
import com.Obatis.core.annotation.validator.IsRange;
import com.Obatis.core.result.ResultInfoOutput;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.NotNull;
import java.util.Date;

public class TestInfo extends ResultInfoOutput {

@ApiModelProperty(value = "类型")
@IsRange(message = "类型只能为1,2,3,4,5", value = "1,2,3,4,5")
private int type;
@ApiModelProperty(value = "用户名")
@NotNull(message = "用户名不能为空")
private String userName;
@ApiModelProperty(value = "出生日期")
private Date date;
@ApiModelProperty(value = "手机号码")
@IsPhoneNumber(message = "请输入正确的手机号码")
private String phoneNumber;
// get、set方法省略
}

打开浏览器输入:http://127.0.0.1:8089/swagger-ui.html,效果如下:



然后点击"Try it out"按钮,输入参数,点击"Execute"按钮即可验证结果。