Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

POST method) in spring rest api

@PostMapping("/rest/v1/books")public ResponseEntity<Book> 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();    }}
Source by www.dariawan.com #
 
PREVIOUS NEXT
Tagged: #POST #spring #rest #api
ADD COMMENT
Topic
Name
2+3 =