我们正在尝试将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类型?
答案 0 :(得分:0)
如果我正确理解您的问题,您希望从CustomRestProtoException
消息类型生成的Java类从java.lang.Exception
开始。我认为你可能实现这一目标的唯一方法是编写一个插件来扩展java代码生成:
http://code.google.com/apis/protocolbuffers/docs/reference/java-generated.html#plugins
另一种更简单的方法是引入一个包装Java Exception子类来保存CustomRestProtoException
对象:
public class CustomRestException extends Exception
{
private CustomRestProtoException exception;
// usual constructors etc.
}