Skip to content

Instantly share code, notes, and snippets.

@jonikarppinen
Last active November 17, 2021 11:49
Show Gist options
  • Select an option

  • Save jonikarppinen/6ade2554946df21db0a6 to your computer and use it in GitHub Desktop.

Select an option

Save jonikarppinen/6ade2554946df21db0a6 to your computer and use it in GitHub Desktop.

Revisions

  1. Joni Karppinen revised this gist Oct 28, 2015. No changes.
  2. Joni Karppinen revised this gist Oct 28, 2015. No changes.
  3. Joni Karppinen revised this gist Oct 28, 2015. No changes.
  4. Joni Karppinen revised this gist Oct 28, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion PdfOrErrorController.java
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,6 @@ public ResponseEntity<byte[]> test() {
    */
    @ExceptionHandler(OriginalExceptionFromAnotherApi.class)
    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
    @ResponseBody
    public ErrorResponse handle(OriginalExceptionFromAnotherApi e) {
    return new ErrorResponse(e.getMessage()); // use message from the original exception
    }
  5. Joni Karppinen revised this gist Oct 28, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions PdfOrErrorController.java
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,8 @@
    * Example of a Spring Boot controller method that can return either:
    * - 200 OK with binary data (application/pdf)
    * - 503 Service Unavailable with JSON error message
    *
    * (Everything defined as static nested classes just for readability of this example.)
    *
    * @author Joni Karppinen
    * @since 2015-10-28
  6. Joni Karppinen revised this gist Oct 28, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions PdfOrErrorController.java
    Original file line number Diff line number Diff line change
    @@ -20,14 +20,14 @@
    public class PdfOrErrorController {

    /**
    * Successful case, return type is byte[] (fake PDF data)
    * Successful case, returns byte[] (fake PDF data)
    * Content type will be application/pdf
    */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResponseEntity<byte[]> test() {
    // Set Content-Type header (+ content disposition, etc, if you want)
    // (Not using "produces", because that depends on request's Accept header including "application/pdf"
    // and otherwise returns 406 Not Acceptable.)
    // (Not using "produces", because that depends on request's Accept header including
    // "application/pdf" and otherwise returns 406 Not Acceptable.)
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    String filename = "output.pdf";
    @@ -37,7 +37,7 @@ public ResponseEntity<byte[]> test() {
    }

    /**
    * Error case, return type is ErrorResponse which Spring automatically converts to JSON (using Jackson)
    * Error case, returns ErrorResponse which Spring automatically converts to JSON (using Jackson)
    * Content type will be application/json
    */
    @ExceptionHandler(OriginalExceptionFromAnotherApi.class)
    @@ -58,7 +58,7 @@ private byte[] getDataOrThrowException() {
    return getBinaryData();
    }
    else {
    throw new OriginalExceptionFromAnotherApi("Failed because the other API is unavailable!");
    throw new OriginalExceptionFromAnotherApi("Failed because the other API is down!");
    }
    }

  7. Joni Karppinen created this gist Oct 28, 2015.
    88 changes: 88 additions & 0 deletions PdfOrErrorController.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    package com.company.project.controllers;

    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;

    import java.util.Random;

    /**
    * Example of a Spring Boot controller method that can return either:
    * - 200 OK with binary data (application/pdf)
    * - 503 Service Unavailable with JSON error message
    *
    * @author Joni Karppinen
    * @since 2015-10-28
    */
    @RestController
    public class PdfOrErrorController {

    /**
    * Successful case, return type is byte[] (fake PDF data)
    * Content type will be application/pdf
    */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResponseEntity<byte[]> test() {
    // Set Content-Type header (+ content disposition, etc, if you want)
    // (Not using "produces", because that depends on request's Accept header including "application/pdf"
    // and otherwise returns 406 Not Acceptable.)
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    String filename = "output.pdf";
    headers.setContentDispositionFormData(filename, filename);

    return new ResponseEntity<>(getDataOrThrowException(), headers, HttpStatus.OK);
    }

    /**
    * Error case, return type is ErrorResponse which Spring automatically converts to JSON (using Jackson)
    * Content type will be application/json
    */
    @ExceptionHandler(OriginalExceptionFromAnotherApi.class)
    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
    @ResponseBody
    public ErrorResponse handle(OriginalExceptionFromAnotherApi e) {
    return new ErrorResponse(e.getMessage()); // use message from the original exception
    }


    private Random random = new Random();

    /**
    * Fake "service layer"
    */
    private byte[] getDataOrThrowException() {
    if (random.nextBoolean()) {
    return getBinaryData();
    }
    else {
    throw new OriginalExceptionFromAnotherApi("Failed because the other API is unavailable!");
    }
    }

    private byte[] getBinaryData() {
    byte[] b = new byte[20];
    random.nextBytes(b);
    return b;
    }

    /**
    * Defines the JSON output format of error responses
    */
    private static class ErrorResponse {
    public String message;

    public ErrorResponse(String message) {
    this.message = message;
    }
    }

    private static class OriginalExceptionFromAnotherApi extends RuntimeException {
    public OriginalExceptionFromAnotherApi(String message) {
    super(message);
    }
    }

    }