码迷,mamicode.com
首页 > 其他好文 > 详细

Swagger2相关配置和使用

时间:2019-11-05 21:35:58      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:etag   str   取数据   height   framework   swagger   rest   访问   request   

所需maven依赖:

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>

Swagger2配置类:

import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2 //开启在线文档
public class SwaggerConfig {

    // 声明api 文档的属性 构建器
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("marrycode")
                .description("xxgc_hui")
                .termsOfServiceUrl("http://www.baidu.com")
                .contact("000")
                .version("1.0.0")
                .build();
    }

    // 核心配置信息
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hui.springboot.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

Controller使用案例:

import com.hui.springboot.bean.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping(value = "/user-test")
public class UserController {

    private List<User> userList = Collections.synchronizedList(new ArrayList<User>());

    // 获取全部用户信息
    @GetMapping("/")
    @ApiOperation(value = "获取数据库中所有用户信息",notes = "获取并返回所有用户信息")
    public List<User> getListUser(){
        return userList;
    }

    // 获取指定用户信息
    @GetMapping("/{id}")
    @ApiOperation(value = "获取指定用户信息",notes = "根据用户id,查找指定用户的所有信息")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer")
    public User getUser(@PathVariable("id") Integer id){
        for (User user : userList) {
            if (user.getId() == id){
                return user;
            }
        }
        return null;
    }

    // 添加用户信息
    @PostMapping("/")
    @ApiOperation(value = "添加用户信息",notes = "获取到用户User参数,添加用户信息")
    @ApiImplicitParam(name = "user",value = "用户实体类信息",required = true,dataType = "User")
    public String createUser(User user){
        userList.add(user);
        return "success";
    }

    // 删除指定用户信息
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除指定用户信息",notes = "根据用户id删除")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer")
    public String deleteUser(@PathVariable("id") Integer id){
        userList.remove(getUser(id));
        return "success";
    }

    // 修改指定用户信息
    @PutMapping("/{id}")
    @ApiOperation(value = "更新指定用户信息",notes = "先由id发起查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "user",value = "用户详细实体信息",required = true,dataType = "User")
    })
    public String updateUser(@PathVariable("id") Integer id, User user){
        for (User user1 : userList) {
            if (user1.getId() == id){
                user1.setName(user.getName());
                user1.setAge(user.getAge());
            }
        }
        return "success";
    }
}

访问接口文档:

localhost:8080/swagger-ui.html

效果如下:

技术图片

 

 技术图片

 

Swagger2相关配置和使用

标签:etag   str   取数据   height   framework   swagger   rest   访问   request   

原文地址:https://www.cnblogs.com/marrycode/p/11801464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!