38-Creating a Complete REST Application Flashcards
What does REST stand for?
REpresentational State Transfer
What does REST emphasize?
Scalability
What does HATEOAS stand for?
Hypermedia As The Engine of Application State
What is the REST specification in Java?
JAX-RS
Implementations of JAX-RS
- Jersey (RI)
- RESTEasy
- Restlet
- CXF
What JAX-RS implementations are supported by Spring?
- Jersey (RI)
- RESTEasy
- Restlet
- CXF
Does Spring MVC implement JAX-RS?
No
What combined request mapping annotations are supported?
- GetMapping
- PostMapping
- PutMapping
- PatchMapping
- DeleteMapping
What request methods must use @RequestMapping?
- HEAD
- OPTIONS
- TRACE
How to use @RequestMapping?
e.g. @RequestMapping(path=”hello”, method=RequestMethod.GET)
What request mappings are implemented automatically by Spring?
- HEAD
2. OPTIONS
What objects help to translate JSON to Java objects and vice versa?
Message converters
Which controller method do we need to return a ResponseEntity?
For POST as we need to return the location of the created object
e.g. return ResponseEntity.created(location).build()
What response should PUT return?
204 no content and empty body
What response should DELETE return?
204 no content and empty body
What response should POST return?
201 created with “Location” in the response header
HTTP status code of Unsupported Media Type
415 - might be due to the request’s indicated Content-Type or Content-Encoding
HTTP status code of cannot generate response body in the requested format
406 Not Acceptable
Due to Accept, Accept-Encoding or Accept-Language
HTTP status code of method not supported
405
By default, what is the result of controller methods?
view name
How to override HTTP status of void method?
@ResponseStatus(HttpStatus.OK) void controllerMethod() {}
How to build the location for POST requests?
Using ServletUriComponentsBuilder e.g. URI location = ServletUriComponentsBuilder .fromCurrentRequestUri() .path("/{resourceId}") .buildAndExpand(resourceId) .toUri(); return ResponseEntity.created(location).build();
What is the return type of a POST controller method?
ResponseEntity
UriComponentsBuilder vs. ServletUriComponentsBuilder
UriComponentsBuilder must hard-code URL
ServletUriComponentsBuilder extends UriComponentsBuilder but allows building from relative API path