Last active
November 17, 2021 11:49
-
-
Save jonikarppinen/6ade2554946df21db0a6 to your computer and use it in GitHub Desktop.
Revisions
-
Joni Karppinen revised this gist
Oct 28, 2015 . No changes.There are no files selected for viewing
-
Joni Karppinen revised this gist
Oct 28, 2015 . No changes.There are no files selected for viewing
-
Joni Karppinen revised this gist
Oct 28, 2015 . No changes.There are no files selected for viewing
-
Joni Karppinen revised this gist
Oct 28, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,6 @@ public ResponseEntity<byte[]> test() { */ @ExceptionHandler(OriginalExceptionFromAnotherApi.class) @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE) public ErrorResponse handle(OriginalExceptionFromAnotherApi e) { return new ErrorResponse(e.getMessage()); // use message from the original exception } -
Joni Karppinen revised this gist
Oct 28, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
Joni Karppinen revised this gist
Oct 28, 2015 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,14 +20,14 @@ public class PdfOrErrorController { /** * 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.) HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.parseMediaType("application/pdf")); String filename = "output.pdf"; @@ -37,7 +37,7 @@ public ResponseEntity<byte[]> test() { } /** * 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 down!"); } } -
Joni Karppinen created this gist
Oct 28, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } } }