38-Creating a Complete REST Application Flashcards

1
Q

What does REST stand for?

A

REpresentational State Transfer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does REST emphasize?

A

Scalability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does HATEOAS stand for?

A

Hypermedia As The Engine of Application State

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the REST specification in Java?

A

JAX-RS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Implementations of JAX-RS

A
  1. Jersey (RI)
  2. RESTEasy
  3. Restlet
  4. CXF
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What JAX-RS implementations are supported by Spring?

A
  1. Jersey (RI)
  2. RESTEasy
  3. Restlet
  4. CXF
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Does Spring MVC implement JAX-RS?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What combined request mapping annotations are supported?

A
  1. GetMapping
  2. PostMapping
  3. PutMapping
  4. PatchMapping
  5. DeleteMapping
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What request methods must use @RequestMapping?

A
  1. HEAD
  2. OPTIONS
  3. TRACE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to use @RequestMapping?

A

e.g. @RequestMapping(path=”hello”, method=RequestMethod.GET)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What request mappings are implemented automatically by Spring?

A
  1. HEAD

2. OPTIONS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What objects help to translate JSON to Java objects and vice versa?

A

Message converters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which controller method do we need to return a ResponseEntity?

A

For POST as we need to return the location of the created object
e.g. return ResponseEntity.created(location).build()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What response should PUT return?

A

204 no content and empty body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What response should DELETE return?

A

204 no content and empty body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What response should POST return?

A

201 created with “Location” in the response header

17
Q

HTTP status code of Unsupported Media Type

A

415 - might be due to the request’s indicated Content-Type or Content-Encoding

18
Q

HTTP status code of cannot generate response body in the requested format

A

406 Not Acceptable

Due to Accept, Accept-Encoding or Accept-Language

19
Q

HTTP status code of method not supported

A

405

20
Q

By default, what is the result of controller methods?

A

view name

21
Q

How to override HTTP status of void method?

A

@ResponseStatus(HttpStatus.OK) void controllerMethod() {}

22
Q

How to build the location for POST requests?

A
Using ServletUriComponentsBuilder
e.g. 
URI location = ServletUriComponentsBuilder
				.fromCurrentRequestUri()
				.path("/{resourceId}")
				.buildAndExpand(resourceId)
				.toUri();
return ResponseEntity.created(location).build();
23
Q

What is the return type of a POST controller method?

A

ResponseEntity

24
Q

UriComponentsBuilder vs. ServletUriComponentsBuilder

A

UriComponentsBuilder must hard-code URL

ServletUriComponentsBuilder extends UriComponentsBuilder but allows building from relative API path

25
Q

What is the difference between RestTemplate.xxxForObject and RestTemplate.xxxForEntity?

A
  1. xxxForObject(String, Class, Object… urlVariables): T
    return response object if any or null if not found
  2. xxxForEntity(String, Class, Object… urlVariables): ResponseEntity
26
Q

How to create a custom RestTemplate?

A

Inject RestTemplateBuilder and call .build()

27
Q

Example of urlVariables in RestTemplate.xxxForObject

A

String uri = “/accounts/{id}”;

User user = restTemplate.xxxForObject(uri, User.class, “1”);

28
Q

How to setup your custom API request?

A

Using RequestEntity
e.g.
request = RequestEntity.post(uri)
.getHeaders().add(HttpHeaders.Authorization, auth)
.contentType(MediaType.APPLICATION_JSON)
.body(newItem);
ResponseEntity response = restTemplate.exchange(request, Void.class);

29
Q

What is WebClient?

A
  1. New client, Support streaming

2. Will Replace RestTemplate

30
Q

How to customize response status for an exception?

A
e.g. @ResponseStatus(HttpStatus.NOT_FOUND)
class OrderNotFound extends RuntimeException {}
from now, when this exception thrown, client will receive 404 instead of 500
31
Q

How to customize response status for existing exceptions (i.e. exceptions from third-party libs)?

A

Create method inside the controller e.g.
@ResponseStatus(HttpStatus.CONFLICT) // 409
@ExceptionHandler({DataIntegrityViolationException.class})
public void conflict() {
// could add the exception, response, etc. as method params
}

32
Q

Example of calling a GET API to get a user by id using WebClient

A
e.g.
client = WebClient.create();
Mono result = client.get()
    .uri("/users/{id}", 1)
    .accept(MediaType.APPLICATION_JSON)
    .retrieve()
    .bodyToMono(User.class);
return result.block();
33
Q

Helper class to build a link for HATEOAS API?

A

Using ControllerLinkBuilder

34
Q

What HypermediaType is supported by Spring HATEOAX API?

A

Only HAL for now

35
Q

Example of building a method with HATEOAS response

A
  1. Enable HATEOAS
    @Configuration
    @EnableHypermediaSupport(type=HypermediaType.HAL)
    public class MyConfig { }
  2. Using @Controller for controller class (not @RestController)
  3. Return Resource
    e.g.
    public @ResponseBody Resource method() {
    Links[] = …; // Some links (see previous slide)
    return new Resource(order, links);
    }
36
Q

What links does Spring HATEOAS support?

A

ATOM and HAL

37
Q

List out 10 arguments in a controller method

A
  1. WebRequest, NativeWebRequest
  2. ServletRequest, ServletResponse
  3. HttpSession
  4. Principal
  5. HttpMethod
  6. Locale
  7. InputStream, Reader, OutputStream, Writer
  8. @PathVariable, @RequestBody, @RequestParam, @MatrixVariable
  9. @ModelAttribute, @RequestAttribute, @SessionAttribute, @RedirectAttribytes
  10. HttpEntity
  11. @RequestHeader