SwaggerConfig

SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
                .apiInfo(apiInfo())
                // 设置哪些接口暴露给Swagger展示
                .select()
                // 扫描所有有注解的api,用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 扫描所有 .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo()
    {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                // 设置标题
                .title("标题:若xbom系统_接口文档")
                .description("swagger的API文档")
                .version("1.0")
                .build();
    }
}

TestController

/**
 * swagger 用户测试方法
 *
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController
{
    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
    {
        users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
        users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public AjaxResult userList()
    {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        return AjaxResult.success(userList);
    }

    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @GetMapping("/{userId}")
    public AjaxResult getUser(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            return AjaxResult.success(users.get(userId));
        }
        else
        {
            return AjaxResult.error("用户不存在");
        }
    }

    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
    @PostMapping("/save")
    public AjaxResult save(UserEntity user)
    {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return AjaxResult.error("用户ID不能为空");
        }
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("更新用户")
    @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
    @PutMapping("/update")
    public AjaxResult update(UserEntity user)
    {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return AjaxResult.error("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId()))
        {
            return AjaxResult.error("用户不存在");
        }
        users.remove(user.getUserId());
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            users.remove(userId);
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error("用户不存在");
        }
    }
}

@ApiModel("用户实体")
class UserEntity
{
    @ApiModelProperty("用户ID")
    private Integer userId;

    @ApiModelProperty("用户名称")
    private String username;

    @ApiModelProperty("用户密码")
    private String password;

    @ApiModelProperty("用户手机")
    private String mobile;

    public UserEntity()
    {

    }

    public UserEntity(Integer userId, String username, String password, String mobile)
    {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.mobile = mobile;
    }

    public Integer getUserId()
    {
        return userId;
    }

    public void setUserId(Integer userId)
    {
        this.userId = userId;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getMobile()
    {
        return mobile;
    }

    public void setMobile(String mobile)
    {
        this.mobile = mobile;
    }
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 技术选型: ###### 后端支持: 主体支持: **JDK1.8**、**Springboot1.4.2** 页面支持:**html** ORM支持: **mybatis3.2.8** ORM插件支持: **mapper3.3**、**PageHelp4.1** 分页 权限支持:**shiro1.2.5** 访问接口支持: **swagger2** JSON支持:**fastjson** 连接池支持:**druid** 缓存支持:**ehcache** 、**guava** 参数验证支持:**jsr303** web容器支持:**内嵌tomcat** maven支持:**maven3.3.9** maven仓库支持:**阿里云** 代码精简与日志支持: **lombok** ###### 前端支持: 主体支持:**jQuery** 风格支持:**layui** 图标支持:**font-awesome** 弹窗支持:**layer** websocket支持:**sockjs** 树结构支持:**ztree** ### 支持情况 1. 修改 **config/application-dev.properties** JDBC 链接属性。 2. tomcat 端口在 **config/application-dev.properties** 更改,默认**8082**。 3. 自动创建数据库,关闭请在配置文件找到 JDBC 链接删除 **&createDatabaseIfNotExist=true**。 4. 自动运行SQL脚本,关闭请注释掉 **spring.datasource.schema**。 5. 默认登录 **admin/admin** 6. 打印后台 SQL 语句。 (打开/关闭 注释该方法 com.zyf.framework.config.MybatisAutoConfiguration.pageHelper) 7. 切换配置文件 **application.properties** 设置参数 8. 访问`http://localhost:8082/api`可以查看项目的接口情况。(打开/关闭 com.zyf.other.api.config.SwaggerConfig)。 9. 使用 html,基本使用 ajax 异步请求,页面不刷新。 10. 基于 shiro 改造的 sso 单机实现,登录生成 token 存储在用户 cookies 中,请求解析 cookies,以解析成功作为标识。 11. 交互上使用 layui,使用第三方功能。 12. 热刷新实体 mapper.xml文件 MapperRefresh.java(打开/关闭 config/application-dev.properties -> mapper.mapper-refresh-enable: true/false) 13. 自动注册枚举到 mybatis,查询出来自动转换枚举。 14. 消息转换未配置,如果需要返回 map 自动转换成 UTF-8 json 及需要保证 JDK8 LocalDateTime 类日期的正确性,请打开 com.zyf.admin.support.config.WebConfiguration.configureMessageConverters 注释部分。 15. 采用 mapper3 pageHelper 插件,因此大部分通用 dao 层及 xml 都不比书写。 16. 后台基本使用 resultFull 风格,前端做的事情比较多。 17. 对象池测试 com.zyf.other.pool.test.RunTest#main ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值