/** * A type that can be used to represent JSON errors that returns a content type of Vnd.Error. * * @author graemerocher * @since 1.1 */ @Produces(MediaType.APPLICATION_VND_ERROR) public class VndError extends JsonError { /** * @param message The message */ public VndError(String message) { super(message); } /** * Used by Jackson. */ @Internal VndError() { } }
/** * Resolve the {@link MediaType} produced by the given type based on the {@link Produces} annotation. * * @param type The type * @return An {@link Optional} {@link MediaType} */ public static Optional<MediaType> fromType(Class<?> type) { Produces producesAnn = type.getAnnotation(Produces.class); if (producesAnn != null) { return Arrays.stream(producesAnn.value()).findFirst().map(MediaType::new); } return Optional.empty(); }
/** * Deprecated. Please use io.micronaut.http.hateoas.AbstractResource * * @author Graeme Rocher * @since 1.0 * @param <Impl> The implementation class. * @deprecated Use {@link io.micronaut.http.hateoas.AbstractResource} instead */ @Produces(MediaType.APPLICATION_HAL_JSON) @Deprecated public abstract class AbstractResource<Impl extends AbstractResource> extends io.micronaut.http.hateoas.AbstractResource<Impl> { }
/** * Resolve the {@link MediaType} produced by the given type based on the {@link Produces} annotation. * * @param type The type * @return An {@link Optional} {@link MediaType} */ public static Optional<MediaType> fromType(Class<?> type) { Produces producesAnn = type.getAnnotation(Produces.class); if (producesAnn != null) { return Arrays.stream(producesAnn.value()).findFirst().map(MediaType::new); } return Optional.empty(); }
/** * Handles exception of type {@link UnsatisfiedArgumentException}. * * @author Graeme Rocher * @since 1.0 */ @Singleton @Produces public class UnsatisfiedArgumentHandler implements ExceptionHandler<UnsatisfiedArgumentException, HttpResponse> { @Override public HttpResponse handle(HttpRequest request, UnsatisfiedArgumentException exception) { JsonError error = new JsonError(exception.getMessage()); error.path('/' + exception.getArgument().getName()); error.link(Link.SELF, Link.of(request.getUri())); return HttpResponse.badRequest(error); } }
/** * Default exception handler for JSON processing errors. * * @author Graeme Rocher * @since 1.0 */ @Produces(MediaType.APPLICATION_JSON) @Singleton public class JsonExceptionHandler implements ExceptionHandler<JsonProcessingException, Object> { @Override public Object handle(HttpRequest request, JsonProcessingException exception) { // TODO: Send JSON back with detailed error MutableHttpResponse<Object> response = HttpResponse.status(HttpStatus.BAD_REQUEST, "Invalid JSON"); JsonError body = new JsonError("Invalid JSON: " + exception.getMessage()); body.link(Link.SELF, Link.of(request.getUri())); response.body(body); return response; } }
/** * Deprecated. Please use io.micronaut.http.hateoas.JsonError * * @author Graeme Rocher * @since 1.0 */ @Produces(MediaType.APPLICATION_JSON) @Deprecated public class JsonError extends io.micronaut.http.hateoas.JsonError { /** * @param message The message */ public JsonError(String message) { super(message); } /** * Used by Jackson. */ @Internal JsonError() { this(null); } }
/** * Deprecated. Please use io.micronaut.http.hateoas.VndError * * @author graemerocher * @since 1.0 */ @Produces(MediaType.APPLICATION_VND_ERROR) @Deprecated public class VndError extends io.micronaut.http.hateoas.VndError { /** * @param message The message */ public VndError(String message) { super(message); } /** * Used by Jackson. */ @Internal VndError() { this(null); } }
@Produces(MediaType.APPLICATION_JSON) public class JsonError extends AbstractResource<JsonError> {
/** * @param vipAddress The vip address * @return A {@link Publisher} with applications info */ @SuppressWarnings("WeakerAccess") @Get("/vips/{vipAddress}") @Produces(single = true) public abstract Publisher<ApplicationInfos> getApplicationVipsInternal(String vipAddress);
/** * @return A {@link Publisher} with applications info. */ @SuppressWarnings("WeakerAccess") @Get("/apps") @Produces(single = true) public abstract Publisher<ApplicationInfos> getApplicationInfosInternal();
@Produces(MediaType.APPLICATION_HAL_JSON) public abstract class AbstractResource<Impl extends AbstractResource> implements Resource {
/** * Handles exceptions of type {@link DuplicateRouteException}. * * @author James Kleeh * @since 1.0 */ @Singleton @Primary @Produces public class DuplicateRouteHandler implements ExceptionHandler<DuplicateRouteException, HttpResponse> { @Override public HttpResponse handle(HttpRequest request, DuplicateRouteException exception) { JsonError error = new JsonError(exception.getMessage()); error.link(Link.SELF, Link.of(request.getUri())); return HttpResponse.badRequest(error); } }
/** * Default handle for {@link ContentLengthExceededException} errors. * * @author Graeme Rocher * @since 1.0 */ @Singleton @Primary @Produces public class ContentLengthExceededHandler implements ExceptionHandler<ContentLengthExceededException, HttpResponse> { @Override public HttpResponse handle(HttpRequest request, ContentLengthExceededException exception) { JsonError error = new JsonError(exception.getMessage()); error.link(Link.SELF, Link.of(request.getUri())); return HttpResponse .status(HttpStatus.REQUEST_ENTITY_TOO_LARGE) .body(error); } }
/** * Handles exception of type {@link ConversionErrorException}. * * @author Graeme Rocher * @since 1.0 */ @Singleton @Primary @Produces public class ConversionErrorHandler implements ExceptionHandler<ConversionErrorException, HttpResponse> { @Override public HttpResponse handle(HttpRequest request, ConversionErrorException exception) { JsonError error = new JsonError(exception.getMessage()); error.path('/' + exception.getArgument().getName()); error.link(Link.SELF, Link.of(request.getUri())); return HttpResponse.badRequest(error); } }
/** * Handles exceptions of type {@link UnsatisfiedRouteException}. * * @author Graeme Rocher * @since 1.0 */ @Singleton @Primary @Produces public class UnsatisfiedRouteHandler implements ExceptionHandler<UnsatisfiedRouteException, HttpResponse> { private static final Logger LOG = LoggerFactory.getLogger(UnsatisfiedRouteHandler.class); @Override public HttpResponse handle(HttpRequest request, UnsatisfiedRouteException exception) { if (LOG.isErrorEnabled()) { LOG.error("{} (Bad Request): {}", request, exception.getMessage()); } JsonError error = new JsonError(exception.getMessage()); error.path('/' + exception.getArgument().getName()); error.link(Link.SELF, Link.of(request.getUri())); return HttpResponse.badRequest(error); } }
/** * Handles exception of type {@link io.micronaut.http.exceptions.HttpStatusException}. * * @author Iván López * @since 1.0 */ @Singleton @Primary @Produces public class HttpStatusHandler implements ExceptionHandler<HttpStatusException, HttpResponse> { @Override public HttpResponse handle(HttpRequest request, HttpStatusException exception) { Object body = exception.getBody() .orElseGet(() -> { JsonError error = new JsonError(exception.getMessage()); error.link(Link.SELF, Link.of(request.getUri())); return error; }); return HttpResponse .status(exception.getStatus()) .body(body); } }
/** * Reads a Key from Consul. See https://www.consul.io/api/kv.html. * * @param applicationName The key * @param profiles The data center * @return A {@link Publisher} that emits a list of {@link ConfigServerResponse} */ @Get("/{applicationName}/{profiles}") @Produces(single = true) @Retryable( attempts = "${" + SpringCloudConfigConfiguration.SpringConfigDiscoveryConfiguration.PREFIX + ".retry-count:3}", delay = "${" + SpringCloudConfigConfiguration.SpringConfigDiscoveryConfiguration.PREFIX + ".retry-delay:1s}" ) @Nonnull Publisher<ConfigServerResponse> readValues( @Nonnull String applicationName, @Nullable String profiles);
@Produces(MediaType.TEXT_EVENT_STREAM) class DefaultEvent<T> implements Event<T> {
@FunctionBean("java/supplier/xml") @Produces(MediaType.TEXT_XML) Supplier<String> getXml() { return () -> "<hello></hello>"; }