use method post to add a record

0

According to what each method has its objective:

  

post to insert records,

     

get to get / show records,

     

put to modify records,

     

delete to delete records

I made the following code, which adds a new book to the bd then I should use the method post, but I did it with the method get, I want to convert it to method post, how to do it?

@RequestMapping(value = "/add/{id}/{name}/{author}/{price}")
    public Book addBook(@PathVariable int id, @PathVariable String name, @PathVariable String author,
            @PathVariable long price) {
        Book book = new Book();
        book.setId(id);
        book.setName(name);
        book.setAuthor(author);
        book.setPrice(price);
        bookService.saveBook(book);
        return book;
    }
    
asked by deluf 01.05.2017 в 00:23
source

2 answers

1

Only add the HTTP method required in the org.springframework.web.bind.annotation.RequestMapping annotation through the method element:

@RequestMapping(value = "/add/{id}/{name}/{author}/{price}",
            method = RequestMethod.POST)
    public Book addBook(@PathVariable int id, @PathVariable String name, @PathVariable String author,
            @PathVariable long price) {
    Book book = new Book();
    book.setId(id);
    book.setName(name);
    book.setAuthor(author);
    book.setPrice(price);
    bookService.saveBook(book);
    return book;
}

Optionally you can send the data in the body of the request and not in the URL:

@RequestMapping(value = "/add",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE
            method = RequestMethod.POST)
    public Book addBook(
            @RequestParam(name = "id", required = false, defaultValue = "0") int id,
            @RequestParam(name = "name", required = true) String name,
            @RequestParam(name = "author", required = true) String author,
            @RequestParam(name = "price", required = true) long price) {
    Book book = new Book();
    book.setId(id);
    book.setName(name);
    book.setAuthor(author);
    book.setPrice(price);
    bookService.saveBook(book);
    return book;
}

Since version 4.3 of Spring it is possible to use the annotation org.springframework.web.bind.annotation.PostMapping , which acts as a shortcut of @RequestMapping(method = RequestMethod.POST) :

@PostMapping(value = "/add",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Book addBook(
            @RequestParam(name = "id", required = false, defaultValue = "0") int id,
            @RequestParam(name = "name", required = true) String name,
            @RequestParam(name = "author", required = true) String author,
            @RequestParam(name = "price", required = true) long price) {
    Book book = new Book();
    book.setId(id);
    book.setName(name);
    book.setAuthor(author);
    book.setPrice(price);
    bookService.saveBook(book);
    return book;
}

Other notes that are shortcuts:

org.springframework.web.bind.annotation.PostMapping
org.springframework.web.bind.annotation.DeleteMapping
org.springframework.web.bind.annotation.GetMapping
org.springframework.web.bind.annotation.PutMapping
org.springframework.web.bind.annotation.PatchMapping
    
answered by 01.05.2017 / 00:54
source
1
  

POST to insert records,

     

GET to get / show records,

     

PUT to modify records,

     

DELETE to delete records

It is correct, however for GET according to the HTTP protocol (which is based on REST) its data entry is through parameters as in your GET method, but in POST it is by means of a body:

Example:

public class Book {
    private int id;
    private String name,
    private String author,
    private long price

//Setters and Getters
}

Controller:

@PostMapping(value = "/add")
    public EntityResponse addBook(@RequestBody Book book) {
    bookService.saveBook(book);
    return new ResponseEntity(book, HttpStatus.CREATED);
}

to create a record also be resolved with 201 instead of 200, in the HTTP status

    
answered by 08.10.2017 в 09:03