GsonMessageBodyProvider.java 文件源码

java
阅读 28 收藏 0 点赞 0 评论 0

项目:GitHub 作者:
@Override
public void writeTo(
    Object t,
    Class<?> type,
    Type genericType,
    Annotation[] annotations,
    MediaType mediaType,
    MultivaluedMap<String, Object> httpHeaders,
    OutputStream entityStream)
    throws IOException,
      WebApplicationException {
  // Special case of unsupported type, where surrounding framework
  // may have, mistakengly, chosen this provider based on media type, but when
  // response will be streamed using StreamingOutput or is already prepared using
  // in the form of CharSequence
  if (t instanceof StreamingOutput) {
    ((StreamingOutput) t).write(entityStream);
    return;
  }
  if (t instanceof CharSequence) {
    // UTF-8 used because it should be considered default encoding for the JSON-family
    // of media types
    OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8);
    writer.append((CharSequence) t);
    writer.flush();
    return;
  }
  // Standard way of handling writing using gson
  try {
    streamer.write(gson, genericType, t, entityStream);
  } catch (IOException ex) {
    exceptionHandler.onWrite(gson, ex);
    throw ex;
  }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号