写点什么

spring boot 整合 Swagger2 构建 API 文档,linux 学习路线图

作者:Java高工P7
  • 2021 年 11 月 10 日
  • 本文字数:1913 字

    阅读完需:约 6 分钟

<groupId>io.springfox</groupId>


<artifactId>springfox-swagger-ui</artifactId>


<version>2.2.2</version>


</dependency>


2.创建 Swagger2 配置类


@Configuration


@EnableSwagger2


public class SwaggerConfig {


@Bean


public Docket apiDoc() {


return docket();


}


private Docket docket() {


return new Docket(DocumentationType.SWAGGER_2).select()


.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))//这里采用包含注解的方式来确定要显示的接口


//.apis(RequestHandlerSelectors.basePackage("com.fz.hr.modules.system.controller")) //这里采用包扫描的方式来确定要显示的接口


.build().ignoredParameterTypes(Errors.class).apiInfo(apiInfo());


}


private ApiInfo apiInfo() {


return new ApiInfoBuilder()


.title("springboot 利用 swagger 构建 api 文档")


.description("简单优雅的 restfun 风格")


.version("1.0")


.build();


}


}


3.给接口添加注解


@RestController


public class UserController {


// 创建线程安全的 Map


static Map<Integer, User> users = Collections.synchronizedMap(new HashMap<Integer, User>());


/**


  • 根据 ID 查询用户

  • @param id

  • @return


*/


@ApiOperation(value="获取用户详细信息", notes="根据 url 的 id 来获取用户详细信息")


@ApiImplicitParam(name = "id", value = "用户 ID", required = true, dataType = "Integer", paramType = "path")


@RequestMapping(value = "user/{id}", method = RequestMethod.GET)


public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){


JsonResult r = new JsonResult();


try {


User user = users.get(id);


r.setResult(user);


r.setStatus("ok");


} catch (Exception e) {


r.setResult(e.getClass().getName() + ":" + e.getMessage());


r.setStatus("error");


e.printStackTrace();


}


return ResponseEntity.ok(r);


}


/**


  • 查询用户列表

  • @return


*/


@ApiOperation(value="获取用户列表", notes="获取用户列表")


@RequestMapping(value = "users", method = RequestMethod.GET)


public ResponseEntity<JsonResult> getUserList (){


JsonResult r = new JsonResult();


try {


List<User> userList = new ArrayList<User>(users.values());


r.setResult(userList);


r.setStatus("ok");


} catch (Exception e) {


r.setResult(e.getClass().getName() + ":" + e.getMessage());


r.setStatus("error");


e.printStackTrace();


}


return ResponseEntity.ok(r);


}


/**


  • 添加用户

  • @param user

  • @return


*/


@ApiOperation(value="创建用户", notes="根据 User 对象创建用户")


@ApiImplicitParam(name = "user", value = "用户详细实体 user", required = true, dataType = "User")


@RequestMapping(value = "user", method = RequestMethod.POST)


public ResponseEntity<JsonResult> add (@RequestBody User user){


JsonResult r = new JsonResult();


try {


users.put(user.getId(), user);


r.setResult(user.getId());


r.setStatus("ok");


} catch (Exception e) {


r.setResult(e.getClass().getName() + ":" + e.getMessage());


r.setStatus("error");


e.printStackTrace();


}


return ResponseEntity.ok(r);


}


/**


  • 根据 id 删除用户

  • @param id

  • @return


*/


@ApiOperation(value="删除用户", notes="根据 url 的 id 来指定删除用户")


@ApiImplicitParam(name = "id", value = "用户 ID", required = true, dataType = "Long", paramType = "path")


@RequestMapping(value = "user/{id}", method = RequestMethod.DELETE)


public ResponseEntity<JsonResult> delete (@PathVariable(value = "id") Integer id){


JsonResult r = new JsonResult();


try {


users.remove(id);


r.setResult(id);


r.setStatus("ok");


} catch (Exception e) {


r.setResult(e.getClass().getName() + ":" + e.getMessage());


r.setStatus("error");


e.printStackTrace();


}


return ResponseEntity.ok(r);


}


/**


  • 根据 id 修改用户信息

  • @param user

  • @return


*/


@ApiOperation(value="更新信息", notes="根据 url 的 id 来指定更新用户信息")


@ApiImplicitParams({


@ApiImplicitParam(name = "id", value = "用户 ID", required = true, dataType = "Long",paramType = "path"),


@ApiImplicitParam(name = "user", value = "用户实体 user", required = true, dataType = "User")


})


@RequestMapping(value = "user/{id}", method = RequestMethod.PUT)


public ResponseEntity<JsonResult> update (@PathVariable("id") Integer id, @RequestBody User u


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


ser){

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
spring boot 整合Swagger2 构建API文档,linux学习路线图