/** * A filter that sets the status on the response before it is returned to the client by the Gateway. * @param status the status to set on the response * @return a {@link GatewayFilterSpec} that can be used to apply additional filters */ public GatewayFilterSpec setStatus(HttpStatus status) { return setStatus(status.toString()); }
public RateLimitExceededException() { super(new ZuulException(HttpStatus.TOO_MANY_REQUESTS.toString(), HttpStatus.TOO_MANY_REQUESTS.value(), null)); } }
/** * @param message message * @return 200 ResponseEntity */ protected ResponseEntity message(String message) { return ResponseEntity.ok(new RespEntity(HttpStatus.OK.toString(), message)); }
public RateLimitExceededException() { super(new ZuulException(HttpStatus.TOO_MANY_REQUESTS.toString(), HttpStatus.TOO_MANY_REQUESTS.value(), null)); } }
@ExceptionHandler(value = Exception.class) @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody public ErrorResponse handleException(Exception e) { LOGGER.error("Internal error", e); return new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.toString()); } }
@RequestMapping("/getRemoteTime") public String getRemoteTime() throws URISyntaxException { LOGGER.info("Request to /getRemoteTime endpoint"); logService.log("Will call remote service"); ResponseEntity<String> forEntity = restTemplate.getForEntity(new URI("http://localhost:" + otherappPort + "/getTime"), String.class); LOGGER.info("Got response code: {}", forEntity.getStatusCode().toString()); return "The remote time is: " + forEntity.getBody(); }
@RequestMapping("/getLocalTime") public String getLocalTime() throws URISyntaxException { LOGGER.info("Request to /getLocalTime endpoint"); logService.log("Will call local service"); ResponseEntity<String> forEntity = restTemplate.getForEntity(new URI("http://localhost:" + appPort + "/getTime"), String.class); LOGGER.info("Got response code: {}", forEntity.getStatusCode().toString()); return "The localtime is: " + forEntity.getBody(); }
public static String buildResponseStatusCode(Method method) { if (method.isAnnotationPresent(ResponseStatus.class)) { ResponseStatus responseStatus = method.getAnnotation(ResponseStatus.class); return responseStatus.value().toString() + " - " + responseStatus.value().getReasonPhrase(); } else { return HttpStatus.OK.toString() + " - " + "OK"; } }
private Map<String, String> errorAttributes(final HttpServletRequest request, final HttpServletResponse response, final HttpStatus httpStatus, final Throwable throwable) { final UUID errorId = Generators.timeBasedGenerator().generate(); LOGGER.error("ErrorId: {} references the following error: ", errorId, throwable); final Map<String, String> errorMap = new HashMap<>(); errorMap.put(ERROR_ID, errorId.toString()); errorMap.put(ERROR, "Oops! Something went wrong!"); errorMap.put(STATUS, httpStatus.toString()); errorMap.put(PATH, request.getRequestURI()); errorMap.put(MESSAGE, "Record the errorId when reporting this issue"); errorMap.put(TIMESTAMP, new Date().toString()); return errorMap; } }
@Override public SendResponse[] sendTemplateEmail(TemplateEmail templateEmail) { StringBuilder url = new StringBuilder(MANDRILL_ADDRESS).append(SEND_TEMPLATE); ResponseEntity<SendResponse[]> response = restTemplate.postForEntity(url.toString(), templateEmail, SendResponse[].class); if (response.getStatusCode().compareTo(HttpStatus.OK) == 0) { if (response.getBody().length > 0) { return response.getBody(); } else { throw new IllegalStateException("Not sent any email."); } } else { throw new IllegalStateException("Unable to send email, status code: " + response.getStatusCode().toString()); } } }
/** * @param response The HTTP response. * @return A "status" tag whose value is the numeric status code. */ public static Tag status(ServerResponse response) { return Tag.of("status", response.statusCode().toString()); } }
@ExceptionHandler(IllegalArgumentException.class) public ResponseEntity<Response> handleBadRequests(HttpServletResponse res, IllegalArgumentException ex) { return new ResponseEntity<>( Response.newBuilder() .withMessage(ex.getMessage()) .withHttpStatus(HttpStatus.BAD_REQUEST.toString()) .build(), HttpStatus.BAD_REQUEST); }
public static String buildResponseStatusCode(Method method) { if (isAnnotated(method, ResponseStatus.class)) { ResponseStatus responseStatus = getAnnotation(method, ResponseStatus.class); return responseStatus.value().toString() + " - " + responseStatus.value().getReasonPhrase(); } else { return HttpStatus.OK.toString() + " - " + "OK"; } }
public Tag status(ServerWebExchange exchange) { HttpStatus status = exchange.getResponse().getStatusCode(); if(status == null) status = HttpStatus.OK; return Tag.of("status", status.toString()); }
/** * Transforming {@link MethodArgumentNotValidException} {@link ValidationException} into server error {@code 500 Internal Server Error} * responses with according validation error message text. * * @return A response object wraps the server result */ @ExceptionHandler({MethodArgumentNotValidException.class, ValidationException.class}) public ResponseEntity<Response> handleValidationException() { return new ResponseEntity<>( Response.newBuilder() .withMessage(translate(ExceptionCodes.VALIDATION_ERROR)) .withHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR.toString()) .build(), HttpStatus.INTERNAL_SERVER_ERROR ); }
/** * Build a response object that signals a not-okay response with a given status {@code code}. * * @param <T> Some type extending the AbstractBase entity * @param code The status code to set as response code * @param msg The error message passed to the caller * @param msgKey The error message key passed to the caller * @param params A set of Serializable objects that are passed to the caller * @return A ResponseEntity with status {@code code} */ protected <T extends AbstractBase> ResponseEntity<Response<T>> buildResponse(HttpStatus code, String msg, String msgKey, T... params) { Response result = Response.newBuilder().withMessage(msg).withMessageKey(msgKey).withHttpStatus(code.toString()).withObj(params).build(); return new ResponseEntity<>(result, code); }
/** * Build a response object that signals a not-okay response with a given status {@code code} and with given http headers. * * @param <T> Some type extending the AbstractBase entity * @param code The status code to set as response code * @param msg The error message passed to the caller * @param msgKey The error message key passed to the caller * @param headers The map of headers. * @param params A set of Serializable objects that are passed to the caller * @return A ResponseEntity with status {@code code} */ protected <T extends AbstractBase> ResponseEntity<Response<T>> buildResponse(HttpStatus code, String msg, String msgKey, MultiValueMap<String, String> headers, T... params) { Response result = Response.newBuilder().withMessage(msg).withMessageKey(msgKey).withHttpStatus(code.toString()).withObj(params).build(); return new ResponseEntity<>(result, headers, code); }
/** * Transform exception into an {@code ResponseEntity} and return it. * * @return The ResponseEntity */ public ResponseEntity<Response> toResponse() { return new ResponseEntity<>(Response.newBuilder().withMessage(getMessage()).withMessageKey(getMessageKey()).withHttpStatus(getStatus().toString()).withObj(getData()).build(), getStatus()); }
/** * Transform exception into an {@code ResponseEntity} and return it. * * @param data Additional implicit data passed to the caller * @return The ResponseEntity */ public ResponseEntity<Response> toResponse(Serializable... data) { return new ResponseEntity<>(Response.newBuilder().withMessage(getMessage()).withMessageKey(getMessageKey()).withHttpStatus(getStatus().toString()).withObj(getData()).build(), getStatus()); }