/** * Created by Trace on 2018-05-16.<br/> * Desc: swagger2配置类 */ @SuppressWarnings({"unused"}) @Configuration @EnableSwagger2 public class Swagger2Config { @Value("${swagger2.enable}") private boolean enable;
@Bean("UserApis") public Docket userApis() { return new Docket(DocumentationType.SWAGGER_2) .groupName("用户模块") .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(PathSelectors.regex("/user.*")) .build() .apiInfo(apiInfo()) .enable(enable); }
@Bean("CustomApis") public Docket customApis() { return new Docket(DocumentationType.SWAGGER_2) .groupName("客户模块") .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(PathSelectors.regex("/custom.*")) .build() .apiInfo(apiInfo()) .enable(enable); }
/** * Created by Trace on 2017-12-01.<br/> * Desc: 用户管理controller */ @SuppressWarnings("unused") @RestController @RequestMapping("/user") @Api(tags = "用户管理") public class UserController { @Resource private UserService userService;
@GetMapping("/query/{id}") @ApiOperation("通过ID查询") @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path") public ResultModel<User> findById(@PathVariable int id) { User user = userService.findById(id); return ResultModel.success("id查询成功", user); }
@GetMapping("/query/username") @ApiOperation("通过用户名称模糊查询") @ApiImplicitParam(name = "userName", value = "用户名称") public ResultModel<List<User>> findByUserName(String userName) { List<User> users = userService.findByUserName(userName); return ResultModel.success(users); }
@PostMapping("/insert") @ApiOperation("新增默认用户") public ResultModel<Integer> insert() { User user = new User(); user.setUserName("zhongshiwen"); user.setNickName("zsw"); user.setRealName("钟仕文"); user.setPassword("zsw123456"); user.setGender("男"); Area area = new Area(); area.setLevel((byte) 5); user.setArea(area); userService.save(user); return ResultModel.success("新增用户成功", user.getId()); }
@PutMapping("/update") @ApiOperation("更新用户信息") public ResultModel<Integer> update(User user) { int row = userService.update(user); return ResultModel.success(row); }
@PutMapping("/update/status") @ApiOperation("更新单个用户状态") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true), @ApiImplicitParam(name = "status", value = "状态", required = true) }) public ResultModel<User> updateStatus(int id, byte status) { User user = userService.updateStatus(id, status); return ResultModel.success(user); }
/** * Created by Trace on 2017-12-01.<br/> * Desc: 接口返回结果对象 */ @SuppressWarnings("unused") @Getter @Setter @ApiModel(description = "返回结果") public final class ResultModel<T> { @ApiModelProperty("是否成功: true or false") private boolean result; @ApiModelProperty("描述性原因") private String message; @ApiModelProperty("业务数据") private T data;