io.micronaut.http.HttpRequest.getBody()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中io.micronaut.http.HttpRequest.getBody()方法的一些代码示例,展示了HttpRequest.getBody()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.getBody()方法的具体详情如下:
包路径:io.micronaut.http.HttpRequest
类名称:HttpRequest
方法名:getBody

HttpRequest.getBody介绍

暂无

代码示例

代码示例来源:origin: micronaut-projects/micronaut-core

private <I> void prepareHttpHeaders(URI requestURI, io.micronaut.http.HttpRequest<I> request, io.netty.handler.codec.http.HttpRequest nettyRequest, boolean permitsBody, boolean closeConnection) {
  HttpHeaders headers = nettyRequest.headers();
  headers.set(HttpHeaderNames.HOST, getHostHeader(requestURI));
  if (closeConnection) {
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
  } else {
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
  }
  if (permitsBody) {
    Optional<I> body = request.getBody();
    if (body.isPresent()) {
      if (!headers.contains(HttpHeaderNames.CONTENT_TYPE)) {
        MediaType mediaType = request.getContentType().orElse(MediaType.APPLICATION_JSON_TYPE);
        headers.set(HttpHeaderNames.CONTENT_TYPE, mediaType);
      }
      if (nettyRequest instanceof FullHttpRequest) {
        FullHttpRequest fullHttpRequest = (FullHttpRequest) nettyRequest;
        headers.set(HttpHeaderNames.CONTENT_LENGTH, fullHttpRequest.content().readableBytes());
      } else {
        headers.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
      }
    } else {
      headers.set(HttpHeaderNames.CONTENT_LENGTH, 0);
    }
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Override
  public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<?> source) {
    Optional<?> body = source.getBody();
    if (!body.isPresent()) {
      //noinspection unchecked
      return BindingResult.EMPTY;
    } else {
      Object o = body.get();
      Optional<T> converted = conversionService.convert(o, context);
      final Optional<ConversionError> lastError = context.getLastError();
      //noinspection OptionalIsPresent
      if (lastError.isPresent()) {
        return new BindingResult<T>() {
          @Override
          public Optional<T> getValue() {
            return Optional.empty();
          }

          @Override
          public List<ConversionError> getConversionErrors() {
            return Collections.singletonList(lastError.get());
          }
        };
      } else {
        return () -> converted;
      }
    }
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

private void traceRequest(io.micronaut.http.HttpRequest<?> request, io.netty.handler.codec.http.HttpRequest nettyRequest) {
  HttpHeaders headers = nettyRequest.headers();
  traceHeaders(headers);
  if (io.micronaut.http.HttpMethod.permitsRequestBody(request.getMethod()) && request.getBody().isPresent() && nettyRequest instanceof FullHttpRequest) {
    FullHttpRequest fullHttpRequest = (FullHttpRequest) nettyRequest;
    ByteBuf content = fullHttpRequest.content();
    if (log.isTraceEnabled()) {
      traceBody("Request", content);
    }
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

Optional<ConvertibleValues> body = source.getBody(ConvertibleValues.class);
if (body.isPresent()) {
  result = doBind(context, body.get(), parameterName);
    } else {
      return () -> source.getBody(argumentType);
  if (source.getBody().isPresent()) {
    Optional<String> text = source.getBody(String.class);
    if (text.isPresent()) {
      return doConvert(text.get(), context);

代码示例来源:origin: io.micronaut/http-client

private <I> void prepareHttpHeaders(URI requestURI, io.micronaut.http.HttpRequest<I> request, io.netty.handler.codec.http.HttpRequest nettyRequest, boolean permitsBody, boolean closeConnection) {
  HttpHeaders headers = nettyRequest.headers();
  headers.set(HttpHeaderNames.HOST, requestURI.getHost());
  if (closeConnection) {
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
  } else {
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
  }
  if (permitsBody) {
    Optional<I> body = request.getBody();
    if (body.isPresent()) {
      if (!headers.contains(HttpHeaderNames.CONTENT_TYPE)) {
        MediaType mediaType = request.getContentType().orElse(MediaType.APPLICATION_JSON_TYPE);
        headers.set(HttpHeaderNames.CONTENT_TYPE, mediaType);
      }
      if (nettyRequest instanceof FullHttpRequest) {
        FullHttpRequest fullHttpRequest = (FullHttpRequest) nettyRequest;
        headers.set(HttpHeaderNames.CONTENT_LENGTH, fullHttpRequest.content().readableBytes());
      } else {
        headers.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
      }
    }
  }
}

代码示例来源:origin: io.micronaut/micronaut-http

@Override
  public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<?> source) {
    Optional<?> body = source.getBody();
    if (!body.isPresent()) {
      //noinspection unchecked
      return BindingResult.EMPTY;
    } else {
      Object o = body.get();
      Optional<T> converted = conversionService.convert(o, context);
      final Optional<ConversionError> lastError = context.getLastError();
      //noinspection OptionalIsPresent
      if (lastError.isPresent()) {
        return new BindingResult<T>() {
          @Override
          public Optional<T> getValue() {
            return Optional.empty();
          }

          @Override
          public List<ConversionError> getConversionErrors() {
            return Collections.singletonList(lastError.get());
          }
        };
      } else {
        return () -> converted;
      }
    }
  }
}

代码示例来源:origin: io.micronaut/micronaut-http-client

private <I> void prepareHttpHeaders(URI requestURI, io.micronaut.http.HttpRequest<I> request, io.netty.handler.codec.http.HttpRequest nettyRequest, boolean permitsBody, boolean closeConnection) {
  HttpHeaders headers = nettyRequest.headers();
  headers.set(HttpHeaderNames.HOST, getHostHeader(requestURI));
  if (closeConnection) {
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
  } else {
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
  }
  if (permitsBody) {
    Optional<I> body = request.getBody();
    if (body.isPresent()) {
      if (!headers.contains(HttpHeaderNames.CONTENT_TYPE)) {
        MediaType mediaType = request.getContentType().orElse(MediaType.APPLICATION_JSON_TYPE);
        headers.set(HttpHeaderNames.CONTENT_TYPE, mediaType);
      }
      if (nettyRequest instanceof FullHttpRequest) {
        FullHttpRequest fullHttpRequest = (FullHttpRequest) nettyRequest;
        headers.set(HttpHeaderNames.CONTENT_LENGTH, fullHttpRequest.content().readableBytes());
      } else {
        headers.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
      }
    } else {
      headers.set(HttpHeaderNames.CONTENT_LENGTH, 0);
    }
  }
}

代码示例来源:origin: io.micronaut/http-client

private void traceRequest(io.micronaut.http.HttpRequest<?> request, io.netty.handler.codec.http.HttpRequest nettyRequest) {
  HttpHeaders headers = nettyRequest.headers();
  traceHeaders(headers);
  if (io.micronaut.http.HttpMethod.permitsRequestBody(request.getMethod()) && request.getBody().isPresent() && nettyRequest instanceof FullHttpRequest) {
    FullHttpRequest fullHttpRequest = (FullHttpRequest) nettyRequest;
    ByteBuf content = fullHttpRequest.content();
    if (log.isTraceEnabled()) {
      traceBody("Request", content);
    }
  }
}

代码示例来源:origin: io.micronaut/micronaut-http-client

private void traceRequest(io.micronaut.http.HttpRequest<?> request, io.netty.handler.codec.http.HttpRequest nettyRequest) {
  HttpHeaders headers = nettyRequest.headers();
  traceHeaders(headers);
  if (io.micronaut.http.HttpMethod.permitsRequestBody(request.getMethod()) && request.getBody().isPresent() && nettyRequest instanceof FullHttpRequest) {
    FullHttpRequest fullHttpRequest = (FullHttpRequest) nettyRequest;
    ByteBuf content = fullHttpRequest.content();
    if (log.isTraceEnabled()) {
      traceBody("Request", content);
    }
  }
}

代码示例来源:origin: io.micronaut/micronaut-http

Optional<ConvertibleValues> body = source.getBody(ConvertibleValues.class);
if (body.isPresent()) {
  result = doBind(context, body.get(), parameterName);
    } else {
      return () -> source.getBody(argumentType);

相关文章