zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    IIS日志字段详解
    Linux CPU监控指标
    PMP 质量管理新7张图
    PMP 质量管理7张图 很形象
    【MVC model 验证失效 】【Unexpected token u in JSON at position 0】【jquery-plugin-validation】
    VS 忽略文件 Git 向远程添加问题
    .Net Core 知识了解:一跨平台的奥秘
    ios 时间解析 差8个小时
    百度定位转腾讯定位
    需求评审会议分析
  • 原文地址:https://www.cnblogs.com/xmanblue/p/7219078.html
Copyright © 2011-2022 走看看