是否有支持/扩展 java.lang.Exception 的 protobuf 消息?
我们正在尝试将 CustomException 表示为 protobuf 格式 -
public class CustomRestException extends RuntimeException {
private CustomRestErrorMessage customRestErrorMessage;
public CustomRestException (CustomRestErrorMessage customRestErrorMessage, Throwable cause) {
super(cause);
this.customRestErrorMessage= customRestErrorMessage;
}
public CustomRestException (CustomRestErrorMessage customRestErrorMessage) {
this.customRestErrorMessage= customRestErrorMessage;
}
}
public class CustomRestErrorMessage implements Serializable {
private String causeMessage = "";
private String errorCode = "";
private String errorMessage = "";
private String errorSubCode = "";
private String stackTrace = "";
}
这是 CustomRestExceptionProtos.proto 文件
option java_package = "com.company.my.exception";
option java_outer_classname = "CustomRestExceptionProtos";
message CustomRestProtoException
{
required CustomRestProtoErrorMessage customRestErrorMessage = 1;
}
message CustomRestProtoErrorMessage
{
required string errorCode = 1;
required string errorMessage = 2;
required string errorSubCode = 3;
required string causeMessage = 4;
required string stackTrace= 5;
}
是否可以将此“CustomRestExceptionProtos”表示为 .proto 文件中的 java.lang.Exception 类型?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的问题,您希望从
CustomRestProtoException
消息类型生成的 Java 类从java.lang.Exception
驱动。我认为实现这一目标的唯一方法是编写一个插件来扩展 java 代码生成:http://code.google.com/apis/protocolbuffers/docs/reference/java- generated.html#plugins
另一种更简单的方法是引入包装器 Java 异常子类持有
CustomRestProtoException
对象:If I understand your question correctly, you want the Java class generated from your
CustomRestProtoException
message type to drive fromjava.lang.Exception
. I think the only way you might achieve this is by writing a plugin to extend the java code generation:http://code.google.com/apis/protocolbuffers/docs/reference/java-generated.html#plugins
An alternate and simpler approach would be to introduce a wrapper Java Exception subclass to hold the
CustomRestProtoException
object: