springboot 2 整合swagger2

  • 1.首先需要在pom.xml进行增加我们需要Swagger2所需要的依赖。
  • <!--swagger依赖-->
  • <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
    </dependency>
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
    </dependency>
  • 2.在 Application中进行声明@EnableSwagger2
  • @SpringBootApplication
    @EnableSwagger2
    public class JbClaimCaseApplication {

    public static void main(String[] args) {
    SpringApplication.run(JbClaimCaseApplication.class, args);
    }
    }
  • 3.声明配置内容的配置 
  • @Configuration
    public class Swagger2 {

    @Bean
    public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .select()
    .apis(RequestHandlerSelectors.basePackage("cn.haitong.test"))
    .paths(PathSelectors.any())
    .build();
    }

    private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
    .title("springboot利用swagger构建api文档")
    .description("简单优雅的restfun风格")
    .termsOfServiceUrl("https://i.cnblogs.com/EditPosts.aspx?opt=1")
    .version("1.0")
    .build();
    }
    }

  • 进行代码配置

    进行标记对应的注释:

    @RestController
    @Api("测试api")
    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 user){
    JsonResult r = new JsonResult();
    try {
    User u = users.get(id);
    u.setUsername(user.getUsername());
    u.setAge(user.getAge());
    users.put(id, u);
    r.setResult(u);
    r.setStatus("ok");
    } catch (Exception e) {
    r.setResult(e.getClass().getName() + ":" + e.getMessage());
    r.setStatus("error");

    e.printStackTrace();
    }
    return ResponseEntity.ok(r);
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String jsonTest() {
    return " hi you!";
    }
    在编写完后,我们进行运行我们的项目后可以访问http://localhost:8080/swagger-ui.html 进行访问Swagger2的相关内容。注意:端口号为server.port定义的,根据自己定义的更换。

  • 注解 描述
    @Api 修饰整个类,描述Controller的作用
    @ApiOperation 描述一个类的一个方法,或者说一个接口
    @ApiParam 单个参数描述
    @ApiModel 用对象来接收参数
    @ApiProperty 用对象接收参数时,描述对象的一个字段
    @ApiResponse HTTP响应其中1个描述
    @ApiResponses HTTP响应整体描述
    @ApiIgnore 使用该注解忽略这个API
    @ApiError 发生错误返回的信息
    @ApiImplicitParam 一个请求参数
    @ApiImplicitParams 多个请求参数

posted on 2019-08-08 09:58  孤独的猫4399  阅读(163)  评论(0编辑  收藏  举报

导航