📜  POST 方法)在 spring rest api - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:23.855000             🧑  作者: Mango

代码示例1
@PostMapping("/rest/v1/books")public ResponseEntity addBook(@RequestBody Book book) throws URISyntaxException {    try {        Book newBook = bookService.save(book);        return ResponseEntity.created(new URI("/rest/v1/books/" + newBook.getId()))                .body(book);    } catch (ResourceAlreadyExistsException ex) {        // log exception first, then return Conflict (409)        logger.error(ex.getMessage());        return ResponseEntity.status(HttpStatus.CONFLICT).build();    } catch (BadResourceException ex) {        // log exception first, then return Bad Request (400)        logger.error(ex.getMessage());        return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();    }}