protobuf接口调用报错:java.nio.charset.MalformedInputException: Input length = 1

在使用protobuf定义的接口API发起HTTP请求时遇到错误:java.nio.charset.MalformedInputException: Input length = 1。问题出现在ServletRequestParser中JsonFormat.Parser的merge方法,疑似与请求中的中文字符有关。当请求包含中文时,CoyoteReader的InputBuffer的ByteBuffer无法正确获取请求消息体,导致protobuf转换失败。
摘要由CSDN通过智能技术生成

  使用protobuf定义的接口api发起http请求报错,日志如下:

[2018-04-16 17:35:16] ERROR ServletRequestParser:46 - IOException:
request=/wlf-hello-war/wlf.hello.helloService/getHelloRank, ex=java.nio.charset.MalformedInputException: Input length = 1

  我们来看ServletRequestParser的报错代码行:

import java.io.IOException;
import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.DatatypeConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;
public class ServletRequestParser {
  private static final Logger logger = LoggerFactory.getLogger(ServletRequestParser.class);

  public static Message parse(HttpServletRequest request, Message prototype) {
    String requestMethod = request.getMethod();
    Descriptors.Descriptor inputType = prototype.getDescriptorForType();
    Message.Builder builder = prototype.newBuilderForType();
    if (HttpMethod.POST.matches(requestMethod)) {
      MediaType contentType = ContentType.BINARY;
      try {
        contentType = MediaType.valueOf(request.getContentType());
      } catch (Exception ex) {
      }
      try {
        if (ContentType.isProtobuf(conten
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值