1. @Controller("/books")
    2. class BooksController {
    3. @Get("/{isbn}")
    4. HttpResponse find(String isbn) {
    5. if (isbn == "1680502395") {
    6. Map<String, Object> m = new HashMap<>()
    7. m.put("status", 401)
    8. m.put("error", "Unauthorized")
    9. m.put("message", "No message available")
    10. m.put("path", "/books/"+isbn)
    11. return HttpResponse.status(HttpStatus.UNAUTHORIZED).body(m)
    12. return HttpResponse.ok(new Book("1491950358", "Building Microservices"))
    13. }
    14. }

    1. @Test
    2. public void afterAnHttpClientExceptionTheResponseBodyCanBeBoundToAPOJO() {
    3. try {
    4. client.toBlocking().exchange(HttpRequest.GET("/books/1680502395"),
    5. Argument.of(Book.class), (1)
    6. Argument.of(CustomError.class)); (2)
    7. } catch (HttpClientResponseException e) {
    8. assertEquals(HttpStatus.UNAUTHORIZED, e.getResponse().getStatus());
    9. Optional<CustomError> jsonError = e.getResponse().getBody(CustomError.class);
    10. assertTrue(jsonError.isPresent());
    11. assertEquals("Unauthorized", jsonError.get().error);
    12. assertEquals("No message available", jsonError.get().message);
    13. assertEquals("/books/1680502395", jsonError.get().path);
    14. }
    15. }
    1. "after an httpclient exception the response body can be bound to a POJO" {
    2. try {
    3. client.toBlocking().exchange(HttpRequest.GET<Any>("/books/1680502395"),
    4. Argument.of(Book::class.java), (1)
    5. Argument.of(CustomError::class.java)) (2)
    6. } catch (e: HttpClientResponseException) {
    7. e.response.status shouldBe HttpStatus.UNAUTHORIZED
    8. }