是否有支持/扩展 java.lang.Exception 的 protobuf 消息?

发布于 2024-12-12 03:01:55 字数 1352 浏览 0 评论 0 原文

我们正在尝试将 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 类型?

We are trying represent a CustomException into protobuf format -

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 = "";
}

And here is the CustomRestExceptionProtos.proto file

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;
 }

Is it possible to represent this "CustomRestExceptionProtos" as type a java.lang.Exception in .proto file ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-12-19 03:01:55

如果我正确理解您的问题,您希望从 CustomRestProtoException 消息类型生成的 Java 类从 java.lang.Exception 驱动。我认为实现这一目标的唯一方法是编写一个插件来扩展 java 代码生成:

http://code.google.com/apis/protocolbuffers/docs/reference/java- generated.html#plugins

另一种更简单的方法是引入包装器 Java 异常子类持有CustomRestProtoException 对象:

public class CustomRestException extends Exception
{
    private CustomRestProtoException exception;

    // usual constructors etc.
}

If I understand your question correctly, you want the Java class generated from your CustomRestProtoException message type to drive from java.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:

public class CustomRestException extends Exception
{
    private CustomRestProtoException exception;

    // usual constructors etc.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文