swagger and restful api 参考
http://git.oschina.net/redArmy/spring-cloud-books/blob/master/spring-cloud-provider-book/src/main/java/com/wujunshen/controller/BookController.java /*** @param book 传入的book对象实例* @return 成功或失败信息,json格式封装*/@PostMapping(value = "/api/books", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)@ApiOperation(value = "添加某本书籍", httpMethod = "POST",notes = "添加成功返回bookId",response = BaseResultVo.class)@ApiResponses(value = {@ApiResponse(code = 200, message = "Success", response = BaseResultVo.class),@ApiResponse(code = 401, message = "Unauthorized"),@ApiResponse(code = 403, message = "Forbidden"),@ApiResponse(code = 404, message = "Not Found"),@ApiResponse(code = 500, message = "Failure")})public BaseResultVo saveBook(@Validated @ApiParam(value = "添加的某本书籍信息", required = true) @RequestBody Book book) {BaseResultVo baseResultVo = new BaseResultVo();baseResultVo.setData(bookService.saveBook(book));baseResultVo.setCode(ResultStatusCode.OK.getCode());baseResultVo.setMessage(ResultStatusCode.OK.getMessage());return baseResultVo;}/*** @return 成功或失败信息,json格式封装*/@GetMapping(value = "/api/books", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)@ApiOperation(value = "查询所有书籍", httpMethod = "GET",notes = "查询所有书籍",response = BaseResultVo.class)@ApiResponses(value = {@ApiResponse(code = 200, message = "Success", response = BaseResultVo.class),@ApiResponse(code = 401, message = "Unauthorized"),@ApiResponse(code = 403, message = "Forbidden"),@ApiResponse(code = 404, message = "Not Found"),@ApiResponse(code = 500, message = "Failure")})public BaseResultVo getBooks() {Books books = bookService.getBooks();BaseResultVo baseResultVo = new BaseResultVo();if ((books != null) && (!books.getBookList().isEmpty())) {baseResultVo.setData(books);baseResultVo.setCode(ResultStatusCode.OK.getCode());baseResultVo.setMessage(ResultStatusCode.OK.getMessage());} else {baseResultVo.setCode(ResultStatusCode.DATA_QUERY_ERROR.getCode());baseResultVo.setData("Query books failed");baseResultVo.setMessage(ResultStatusCode.DATA_QUERY_ERROR.getMessage());}return baseResultVo;}/*** @param bookId 传入的bookId* @return 成功或失败信息,json格式封装*/@GetMapping(value = "/api/books/{bookId:[0-9]*}")@ApiOperation(value = "查询某本书籍", httpMethod = "GET",notes = "根据bookId,查询到某本书籍",response = BaseResultVo.class)@ApiResponses(value = {@ApiResponse(code = 200, message = "Success", response = BaseResultVo.class),@ApiResponse(code = 401, message = "Unauthorized"),@ApiResponse(code = 403, message = "Forbidden"),@ApiResponse(code = 404, message = "Not Found"),@ApiResponse(code = 500, message = "Failure")})public BaseResultVo getBook(@ApiParam(value = "书籍ID", required = true) @PathVariable("bookId") Integer bookId) {LOGGER.info("请求参数bookId值:{}", bookId);Book book = bookService.getBook(bookId);BaseResultVo baseResultVo = new BaseResultVo();if (book != null) {LOGGER.info("查询到书籍ID为{}的书籍", bookId);baseResultVo.setData(book);baseResultVo.setCode(ResultStatusCode.OK.getCode());baseResultVo.setMessage(ResultStatusCode.OK.getMessage());} else {LOGGER.info("没有查询到书籍ID为{}的书籍", bookId);baseResultVo.setCode(ResultStatusCode.DATA_QUERY_ERROR.getCode());baseResultVo.setData("Query book failed id=" + bookId);baseResultVo.setMessage(ResultStatusCode.DATA_QUERY_ERROR.getMessage());}return baseResultVo;}@PutMapping(value = "/api/books/{bookId:[0-9]*}")@ApiOperation(value = "更新某本书籍", httpMethod = "PUT",notes = "更新的某本书籍信息",response = BaseResultVo.class)@ApiResponses(value = {@ApiResponse(code = 200, message = "Success", response = BaseResultVo.class),@ApiResponse(code = 401, message = "Unauthorized"),@ApiResponse(code = 403, message = "Forbidden"),@ApiResponse(code = 404, message = "Not Found"),@ApiResponse(code = 500, message = "Failure")})public BaseResultVo updateBook(@NotNull @ApiParam(value = "要更新的某本书籍ID", required = true) @PathVariable("bookId") Integer bookId, @Validated @NotNull @ApiParam(value = "要更新的某本书籍信息", required = true) @RequestBody Book book) {LOGGER.info("请求参数bookId值:{}", bookId);BaseResultVo baseResultVo = new BaseResultVo();if (bookId == null && book == null) {baseResultVo.setCode(ResultStatusCode.DATA_INPUT_ERROR.getCode());baseResultVo.setMessage(ResultStatusCode.DATA_INPUT_ERROR.getMessage());return baseResultVo;}if (bookService.getBook(bookId) == null) {baseResultVo.setCode(ResultStatusCode.DATA_QUERY_ERROR.getCode());baseResultVo.setData("book id=" + bookId + " not existed");baseResultVo.setMessage(ResultStatusCode.DATA_QUERY_ERROR.getMessage());return baseResultVo;}Book updatedBook = bookService.updateBook(book);if (updatedBook != null) {baseResultVo.setData(updatedBook);baseResultVo.setCode(ResultStatusCode.OK.getCode());baseResultVo.setMessage(ResultStatusCode.OK.getMessage());} else {baseResultVo.setCode(ResultStatusCode.DATA_UPDATED_ERROR.getCode());baseResultVo.setData("Update book failed id=" + book.getBookId());baseResultVo.setMessage(ResultStatusCode.DATA_UPDATED_ERROR.getMessage());}return baseResultVo;}@DeleteMapping(value = "/api/books/{bookId:[0-9]*}")@ApiOperation(value = "删除某本书籍信息", httpMethod = "DELETE",notes = "删除某本书籍信息",response = BaseResultVo.class)@ApiResponses(value = {@ApiResponse(code = 200, message = "Success", response = BaseResultVo.class),@ApiResponse(code = 401, message = "Unauthorized"),@ApiResponse(code = 403, message = "Forbidden"),@ApiResponse(code = 404, message = "Not Found"),@ApiResponse(code = 500, message = "Failure")})public BaseResultVo deleteBook(@ApiParam(value = "要删除的某本书籍ID", required = true) @PathVariable("bookId") Integer bookId) {BaseResultVo baseResultVo = new BaseResultVo();if (bookService.deleteBook(bookId) == 1) {baseResultVo.setData("Deleted book id=" + bookId);baseResultVo.setCode(ResultStatusCode.OK.getCode());baseResultVo.setMessage(ResultStatusCode.OK.getMessage());} else {baseResultVo.setCode(ResultStatusCode.DATA_DELETED_ERROR.getCode());baseResultVo.setData("Deleted book failed id=" + bookId);baseResultVo.setMessage(ResultStatusCode.DATA_DELETED_ERROR.getMessage());}return baseResultVo;}
swagger and restful api 参考的更多相关文章
- Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
- 使用swagger作为restful api的doc文档生成
初衷 记得以前写接口,写完后会整理一份API接口文档,而文档的格式如果没有具体要求的话,最终展示的文档则完全决定于开发者的心情.也许多点,也许少点.甚至,接口总是需要适应新需求的,修改了,增加了,这份 ...
- 使用swagger作为restful api的doc文档生成——从源码中去提取restful URL接口描述文档
初衷 记得以前写接口,写完后会整理一份API接口文档,而文档的格式如果没有具体要求的话,最终展示的文档则完全决定于开发者的心情.也许多点,也许少点.甚至,接口总是需要适应新需求的,修改了,增加了,这份 ...
- 基于jeesite的cms系统(三):使用RESTful API在前端渲染数据
使用RESTful API可以更好的开发前后分离的应用,后面一节会介绍使用模版引擎Beetl开发后端渲染的应用. 一.配置Swagger(Api 接口文档) 1.使用系统自带 拷贝jeesite-mo ...
- Yii2 Restful API 原理分析
Yii2 有个很重要的特性是对 Restful API的默认支持, 通过短短的几个配置就可以实现简单的对现有Model的RESTful API 参考另一篇文章: http://www.cnblogs. ...
- Yii2+Swagger搭建RESTful风格的API项目
在现有的Advanced Template上搭建RESTful API项目的步骤: 本案例前提说明: 本例中不使用\yii\rest\ActiveController自动创建的API,而是自定义一个A ...
- ****RESTful API 设计最佳实践(APP后端API设计参考典范)
http://blog.jobbole.com/41233/ 背景 目前互联网上充斥着大量的关于RESTful API(为方便,下文中“RESTful API ”简写为“API”)如何设计的文章,然而 ...
- spring boot之使用springfox swagger展示restful的api doc
摘要 springfox swagger展示restful的api doc, swagger is A POWERFUL INTERFACE TO YOUR API. 新增文件: import org ...
- 使用swagger 生成 Flask RESTful API
使用swagger 生成 Flask RESTful API http://www.voidcn.com/article/p-rcvzjvpf-e.html swagger官网 https://swa ...
随机推荐
- 转: telnet命令学习
1.每天一个linux命令(58):telnet命令 转自: http://www.cnblogs.com/peida/archive/2013/03/13/2956992.html telnet命令 ...
- 项目加入 TFS报错
新建一个项目,然后在解决方案上右击,选择Add solution to source control的时候,总是失败,output窗口中出现的错误信息如下: An error was raised ...
- CSS半透明兼容写法
filter: Alpha(opacity=10); -moz-opacity:0.1; opacity:0.1; 例如: background:#A5CD40; filter: Alpha(opac ...
- DispatcherTimer
1.IsEnabled 表示计时器是否已经启动. 2.DispatcherTimer处于当前线程的管理,不会新建一个线程专门用于计时操作,也就是说,当前线程可能会阻塞计时器.因此,Dispatcher ...
- 新浪微博SSO登陆机制(转载)
原文地址: http://www.cnblogs.com/AloneSword/p/3840548.html 最近在使用sina微博时,经常性交替使用 weibo.com 和 t.sina.cm.cn ...
- VMware12 中安装MS-DOS 7.10
按一下步骤进行安装: 选择虚拟机,然后如下图选择“ 编辑虚拟机设置 ”. 弹出的编辑框中,选择“CD/DVD”中的“使用ISO镜像文件”,然后选择“浏览”,打开MS-DOS7.10.iso的ISO镜像 ...
- more命令(转)
原文:http://www.cnblogs.com/peida/archive/2012/11/02/2750588.html more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示 ...
- Java从零开始学八(循环结构)
一.循环结构 循环结构则是根据判断条件的成立与否,决定程序段落的执行次数,而这个程序段落就称为循环主体.
- 为什么学习Python及Python环境安装
大部分人在工作中可能是以c/c++.java之类的语言为主.这也可能是我们接触的第一个开发语言,这类语言一般有丰富地类库.高效地运行速率.灵活地组合控制,须要经过编译在运行.适用于大型的项目proje ...
- 算法笔记_193:历届试题 连号区间数(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 小明这些天一直在思考这样一个奇怪而有趣的问题: 在1~N的某个全排列中有多少个连号区间呢?这里所说的连号区间的定义是: 如果区间[L, R ...