本文整理了Java中io.micronaut.http.HttpResponse
类的一些代码示例,展示了HttpResponse
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse
类的具体详情如下:
包路径:io.micronaut.http.HttpResponse
类名称:HttpResponse
[英]Common interface for HTTP response implementations.
[中]HTTP响应实现的公共接口。
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* Perform an HTTP request for the given request object emitting the full HTTP response from returned
* {@link org.reactivestreams.Publisher} and converting the response body to the specified type.
*
* @param request The {@link HttpRequest} to execute
* @param bodyType The body type
* @param errorType The error type
* @param <I> The request body type
* @param <O> The response body type
* @param <E> The error type
* @return A result of the given type or null the URI returns a 404
* @throws HttpClientResponseException if an error status is returned
*/
@SuppressWarnings("unchecked")
default <I, O, E> O retrieve(HttpRequest<I> request, Argument<O> bodyType, Argument<E> errorType) {
HttpResponse<O> response = exchange(request, bodyType, errorType);
if (HttpStatus.class.isAssignableFrom(bodyType.getType())) {
return (O) response.getStatus();
} else {
return response
.getBody()
.orElseThrow(() -> new HttpClientResponseException(
"Empty body",
response
));
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
private MutableHttpResponse<Object> forStatus(AnnotationMetadata annotationMetadata) {
HttpStatus status = HttpStatus.OK;
if (annotationMetadata.hasAnnotation(Status.class)) {
status = annotationMetadata.getValue(Status.class, HttpStatus.class).orElse(status);
}
return HttpResponse.status(status);
}
代码示例来源:origin: micronaut-projects/micronaut-core
HttpMethod methodToMatch = preflight ? headers.getFirst(ACCESS_CONTROL_REQUEST_METHOD, HttpMethod.class).orElse(requestMethod) : requestMethod;
if (allowedMethods.stream().noneMatch(method -> method.equals(methodToMatch))) {
return Optional.of(HttpResponse.status(HttpStatus.FORBIDDEN));
.allMatch(header -> allowedHeaders.stream()
.anyMatch(allowedHeader -> allowedHeader.equals(header.toString().trim())))) {
return Optional.of(HttpResponse.status(HttpStatus.FORBIDDEN));
MutableHttpResponse<Object> ok = HttpResponse.ok();
handleResponse(request, ok);
return Optional.of(ok);
代码示例来源:origin: micronaut-projects/micronaut-core
private Class<?> getErrorType(HttpResponse<?> response) {
Optional<MediaType> contentType = response.getContentType();
Class<?> errorType = null;
if (contentType.isPresent() && response.getStatus().getCode() > 399) {
MediaType mediaType = contentType.get();
errorType = errorDecoder.getErrorType(mediaType);
}
return errorType;
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* @return The HTTP status
*/
default HttpStatus status() {
return getStatus();
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* @return The body or null
*/
default @Nullable B body() {
return getBody().orElse(null);
}
代码示例来源:origin: micronaut-projects/micronaut-core
return Flowable.just(HttpResponse.serverError());
}).map(response -> Collections.singletonMap(originalURI, response.getStatus()));
}).subscribe(uriToStatusMap -> {
Map.Entry<URI, HttpStatus> entry = uriToStatusMap.entrySet().iterator().next();
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public HttpResponse handle(HttpRequest request, OutOfStockException exception) {
return HttpResponse.ok(0);
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
request,
nettyHttpRequest,
HttpResponse.status(status),
status.getReason()
);
request,
nettyHttpRequest,
HttpResponse.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE),
"Content Type [" + contentType + "] not allowed. Allowed types: " + existingRouteConsumes);
return;
request,
nettyHttpRequest,
HttpResponse.notAllowed(existingRouteMethods),
"Method [" + httpMethod + "] not allowed. Allowed methods: " + existingRouteMethods);
return;
request,
nettyHttpRequest,
HttpResponse.status(HttpStatus.BAD_REQUEST),
"Not a WebSocket request");
} else {
代码示例来源:origin: micronaut-projects/micronaut-examples
@Post("/send")
public HttpResponse send(@Valid @Body Email email) {
log.info(email.toString());
if ( emailService == null ) {
log.warn("Email service not injected");
return HttpResponse.serverError();
}
emailService.send(email);
return HttpResponse.ok();
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
@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);
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
private MutableHttpResponse errorResultToResponse(Object result) {
MutableHttpResponse<?> response;
if (result == null) {
response = io.micronaut.http.HttpResponse.serverError();
} else if (result instanceof io.micronaut.http.HttpResponse) {
response = (MutableHttpResponse) result;
} else {
response = io.micronaut.http.HttpResponse.serverError()
.body(result);
MediaType.fromType(result.getClass()).ifPresent(response::contentType);
}
return response;
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* Return the first value for the given header or null.
*
* @param name The name
* @return The header value
*/
default @Nullable String header(@Nullable CharSequence name) {
if (name == null) {
return null;
}
return getHeaders().get(name);
}
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* @return The HTTP status reason phrase
*/
default String reason() {
return getStatus().getReason();
}
代码示例来源:origin: micronaut-projects/micronaut-core
@SuppressWarnings("MagicNumber")
private void initResponse(HttpResponse<?> response) {
Class<?> errorType = getErrorType(response);
if (errorType != null) {
response.getBody(errorType);
} else {
response.getBody(String.class);
}
}
代码示例来源:origin: io.micronaut/http-client
return Flowable.just(HttpResponse.serverError());
}).map(response -> Collections.singletonMap(originalURI, response.getStatus()));
}).subscribe(uriToStatusMap -> {
Map.Entry<URI, HttpStatus> entry = uriToStatusMap.entrySet().iterator().next();
代码示例来源:origin: micronaut-projects/micronaut-core
@Override
public HttpResponse handle(HttpRequest request, OutOfStockException exception) {
return HttpResponse.ok(0);
}
}
代码示例来源:origin: io.micronaut/micronaut-http-client
private Class<?> getErrorType(HttpResponse<?> response) {
Optional<MediaType> contentType = response.getContentType();
Class<?> errorType = null;
if (contentType.isPresent() && response.getStatus().getCode() > 399) {
MediaType mediaType = contentType.get();
errorType = errorDecoder.getErrorType(mediaType);
}
return errorType;
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
@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);
}
}
代码示例来源:origin: micronaut-projects/micronaut-core
@SuppressWarnings("unchecked")
private void writeDefaultErrorResponse(ChannelHandlerContext ctx, NettyHttpRequest nettyHttpRequest, Throwable cause) {
logException(cause);
MutableHttpResponse<?> error = io.micronaut.http.HttpResponse.serverError()
.body(new JsonError("Internal Server Error: " + cause.getMessage()));
subscribeToResponsePublisher(
ctx,
MediaType.APPLICATION_JSON_TYPE,
new AtomicReference<>(nettyHttpRequest),
Flowable.just(error)
);
}
内容来源于网络,如有侵权,请联系作者删除!