登录  /  注册
首页 > Java > java教程 > 正文
分享Swagger2+validator的介绍
零下一度
发布: 2017-07-17 13:37:48
原创
2330人浏览过

介绍:

swagger是一个用于描述项目和文档RESTful api。

这里的规范定义了一组描述一个API所需的文件格式。 Swagger-UI项目所使用的这些文件可以显示API和Swagger-Codegen生成客户在不同的语言。 额外的工具也可以利用生成的文件,比如测试工具。

 

Swagger2:
登录后复制
@Configuration
@EnableSwagger2public class Swagger2
{
    @Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dj.edi.web"))
                .paths(PathSelectors.any())
                .build();
    }private ApiInfo apiInfo() {return new ApiInfoBuilder()
                .title("EID 用户 CRUD")
                .description("EID 用户 CRUD")
                .version("1.0")
                .build();
    }

}
登录后复制
Application:
登录后复制
@SpringBootApplication
@Import(springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class)public class ComEdiOrderUserApplication
{public static void main(String[] args) {SpringApplication.run(ComEdiOrderUserApplication.class, args);}

}
登录后复制
UserApiController:
登录后复制

登录后复制
登录后复制
@RestController
@RequestMapping("/v1/user")public class UserApiController
{private static final Logger LOGGER = LoggerFactory.getLogger(UserApiController.class);

    @Autowiredprivate ClientUsersRepository repository;

    @ApiOperation(value = "获取所有用户数据")
    @RequestMapping(value = "/list", method = RequestMethod.GET)public ResponseEntity<List<ClientUsers>> getClientUsersList() {try {return ResponseEntity.ok(repository.findAll());
        } catch (Exception e) {
            LOGGER.info(" 获取所有用户数据异常 " + e.getMessage(), e);return ResponseEntity.status(500).body(null);
        }
    }

    @ApiOperation(value = "获取用户数据")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)public ResponseEntity<ClientUsers> getClientUsers(@PathVariable String id) {try {return ResponseEntity.ok(repository.findOne(id));
        } catch (Exception e) {
            LOGGER.info(" 获取用户数据  " + id + "  数据异常 " + e.getMessage(), e);return ResponseEntity.status(500).body(null);
        }
    }

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "users", value = "用户详细实体user", required = true, dataType = "ClientUsers", paramType = "body")
    @RequestMapping(method = RequestMethod.POST)public ResponseEntity<ClientUsers> createUser(@Valid @RequestBody ClientUsers users) {try {

            users.setId(ObjectId.get().toString());return ResponseEntity.ok(repository.save(users));

        } catch (Exception e) {
            LOGGER.info(" 创建用户  " + users + "  数据异常 " + e.getMessage(), e);return ResponseEntity.status(500).body(null);
        }
    }

    @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "ClientUsers", paramType = "body")
    })
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)public ResponseEntity<ClientUsers> updateUser(@PathVariable("id") String id,@Valid @RequestBody ClientUsers user) {try {
            user.setId(id);return ResponseEntity.ok(repository.save(user));
        } catch (Exception e) {
            LOGGER.info(" 更新用户  " + user + "  数据异常 " + e.getMessage(), e);return ResponseEntity.status(500).body(null);
        }
    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)public ResponseEntity<String> deleteUser(@PathVariable String id) {try {
            repository.delete(id);return ResponseEntity.ok("ok");
        } catch (Exception e) {
            LOGGER.info(" 删除用户  " + id + "  数据异常 " + e.getMessage(), e);return ResponseEntity.status(500).body(null);
        }
    }
}
登录后复制

登录后复制
登录后复制
ClientUsersRepository:
登录后复制
@Componentpublic interface ClientUsersRepository extends MongoRepository<ClientUsers, String>{
    ClientUsers findByips(String ip);
    ClientUsers findByclientFlag(String clientFlag);
}
登录后复制
ClientUsers:
登录后复制
@Datapublic class ClientUsers implements Serializable
{

    @Idprivate String id;/** * 用户名称     */@NotBlank(message = "用户名称 不能为空")
    @Pattern(regexp = "^(?!string)",message = "不能是 stirng")private String userName;/** * ip     */@NotNull(message = "ip 至少需要个")private List<String> ips;/** * 标识     */@NotBlank(message = " 标识 不能为空")
    @Pattern(regexp = "^(?!string)",message = "不能是 stirng")private String clientFlag;/** * 客户服务ID     */@NotBlank(message = "客户服务ID 不能为空")
    @Pattern(regexp = "^(?!string)",message = "不能是 stirng")private String checkID;
}
登录后复制

有哪里不好的希望指正
登录后复制
 
登录后复制

以上就是分享Swagger2+validator的介绍的详细内容,更多请关注php中文网其它相关文章!

相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学