0

I have done some reading and research on @RequestBody but I am still unable to wrap my thick head around the purpose of it.

Given the scenario:

  • RESTful API takes only bookName from a user and returns the authorName of the book.
  • The @RequestBody annotation is added on the getAuthor method which takes the bookName as a parameter.

From what I read in a similar question answered, how does that apply in this scenario? What does it mean by converting A to B and then B to A so that users can see it as a JSON object. I'm so confused!

Community
  • 1
  • 1
johnwick0831
  • 918
  • 1
  • 12
  • 24

1 Answers1

4

From the javadoc:

Annotation indicating a method parameter should be bound to the body of the web request. The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request. Optionally, automatic validation can be applied by annotating the argument with @Valid.

In your example there is no point in using @RequestBody. You want to get the author name corresponding to a book

You send GET/books/myBook/author

The Controller would be

@RestController
public class BooksRestController{

    @RequestMapping(value="/books/{bookName}/author", method=RequestMethod.GET)
    public Map<String,String> getAuthor(@PathVariable bookName){
        ...
        Map<String,String> author = new HashMap<String,String>();
        author.put("author",bookAuthor);
        return author;
    }
}

The response would be {"author":"whoever"}.

Now imagine that you want to create a book. That's when you would use @RequestBody, to automatically parse the JSON sent in the HTTP POST body into a Book object.

Request: POST /books - Body {"name" : "SomeBook", "author":"SomeAuthor"}

Controller:

@RestController
public class BooksRestController{

    @RequestMapping(value="/books", method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public Book createBook(@RequestBody Book book){
        ...
        //save book
        book = bookService.save(book);
        return book;
        //Book is going to be serialized as JSON because of the implicit 
        //@ResponseBody annotation from @RestController
    }

Book.class:

public class Book{
    private String name;
    private String author;
    //getters setters;
}
codependent
  • 23,193
  • 31
  • 166
  • 308
  • Thank you very much for your explanation, it was clear and concise. You mentioned that: "Now imagine that you want to create a book. That's when you would use @RequestBody, to automatically parse the JSON sent in the HTTP POST body into a Book object." Does it also convert it back to a JSON object and display it back on the browser / return to the user? – johnwick0831 Sep 20 '16 at 20:55
  • Nope, to achieve that yo need to specify `@ResponseBody` in the return parameter of the method. However, as you can see in the example, the controller is anotated with `@RestController`, which implicitly assumes that annotation (from the javadoc: `Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default.`). I you were using `@Controller` instead, you would have to add the annotation yourself. – codependent Sep 21 '16 at 06:47