onError总是内部服务器错误

时间:2018-02-01 21:32:19

标签: java httpresponse grpc grpc-java

我正在运行java grpc服务器。我想在客户端调用不存在的资源时发现404资源未找到错误,而是在休息调用中收到500服务器错误,在grpc客户端调用中收到200错误。

来自休息端点的呼叫者响应如下:

"status":500,"error":"Internal Server Error","exception":"io.grpc.StatusRuntimeException","message":"NOT_FOUND: Mapping not found."

服务器产生:

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is io.grpc.StatusRuntimeException: NOT_FOUND: Mapping not found.] with root cause
    io.grpc.StatusRuntimeException: NOT_FOUND: Mapping not found.

我想要的是客户端获取404错误代码。这可能吗?

代码:

@Override
public void getMyObjectData(DataRequest request, StreamObserver<DataResponse> responseObserver) {
    String input = request.getData();
    if(/* Check if input exists in back end */) {
        String data = /* Retrieve data */
        DataResponse response = DataResponse
                                  .newBuilder()
                                  .setData(data)
                                  .build();

        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
    else {
        responseObserver.onError(Status.NOT_FOUND.withDescription("Mapping not found.").asException());
    }
}

使用gRPC客户端返回以下内容:

headers=GrpcHttp2ResponseHeaders[:status: 200, content-type: application/grpc, grpc-status: 5, grpc-message: Mapping not found.]

客户端代码:

public void getData(String input) {
    DataRequest request = DataRequest
                                .newBuilder()
                                .setInput(input)
                                .build();
    DataResponse response;
    try {
        response = blockingStub.getData(request);
    }
    catch (StatusRuntimeException e) {
        LOG.error("RPC failed: {}", e.getMessage());
        return;
    }
}

0 个答案:

没有答案