gpt4 book ai didi

java - REST 错误响应和客户端-服务器 POJO 序列化

转载 作者:行者123 更新时间:2023-11-29 05:15:49 25 4
gpt4 key购买 nike

给定以下 POJO:

public class BaseEntity {
public Long id;
// ctor, getter & setter
}

public class Widget extends BaseEntity {
public String fizz;
public Boolean isBuzz;
// ctor, getters & setters
}

我有以下客户端 API,用于针对远程 REST 服务的 CRUDding Widget 实例:

public class WidgetServiceClient {
public void createWidget(Widget w) {
// RESTful call: POST localhost/widgets
}

public Widget getWidgetById(Long id) {
// RESTful call: GET localhost/widgets/{id}
}
}

并且公开了以下 RESTful 服务端点:

public class WidgetService {
// Web server passes POST localhost/widgets/ calls to this method.
public Widget createWidget(Widget w) {
// Create the 'w' widget in the DB and return it with its DB-generated ID.
}

// Web server passes GET localhost/widgets/{id} calls to this method.
public Widget getWidgetById(Long id) {
// Ask the DB for the Widget with the passed-in 'id'. If it exist return it.
// Otherwise return NULL.
}
}

假设我已经弄明白了将 Widget 实例序列化/反序列化为 JSON 或从 JSON 序列化/反序列化的“魔力”。

这个设计很棒,除了有一个服务器端Exception,我想以 RESTfull 方式与客户端通信。

我的第一个想法是修改 BaseEntity 以拥有一个 Throwable 可用于将服务器端错误传回客户端:

public class BaseEntity {
public Long id;
public Throwable error;
// ctor, getters & setters
}

然后:

public class WidgetService {
// Web server passes POST localhost/widgets/ calls to this method.
public Widget createWidget(Widget w) {
try {
// Create the 'w' widget in the DB and return it with its DB-generated ID.
} catch(Throwable t) {
w.setError(t);
}

return w;
}

// Web server passes GET localhost/widgets/{id} calls to this method.
public Widget getWidgetById(Long id) {
Widget w = new Widget();
try {
// Ask the DB for the Widget with the passed-in 'id'. If it exist return it.
// Otherwise return NULL.
} catch(Throwable t) {
w.setError(t);
}

return w;
}
}

但这感觉很笨拙/老套,我想知道 Javaland 的其他居民是否已经想出更好的方法/策略来解决这个问题。我碰巧正在使用 Jersey & Jackson 进行 REST/序列化,但我认为该解决方案可能应该与框架无关。

当服务返回 NULL 时,它也无济于事,这可能会发生。

所以我问:我怎样才能以 REST 方式在客户端和服务器之间来回传递 Widget 实例,但仍然允许服务器返回 NULL 和 Exceptions/Throwables?

最佳答案

我建议将模型响应和错误响应分开 - 关注点分离。假设 Jersey,Jersey 知道如何从 WebApplicationExceptions 中吸取响应,允许您在错误响应中提供丰富的错误信息,帮助您的客户了解哪里出了问题。

作为一个简短的示例,您可以将签名保留为返回 Widget 并在出错时抛出 WebApplicationException 派生类。您的客户将在成功时收到 200 Widget,在异常时收到 404 Response(例如下面)。

// Web server passes GET localhost/widgets/{id} calls to this method.
public Widget getWidgetById(Long id) {
Widget w = new Widget();
try {
// Ask the DB for the Widget with the passed-in 'id'. If it exist return it.
// Otherwise return NULL.
} catch(NotFoundException e) {
throw new NotFoundException(Response.status(Response.Status.NOT_FOUND)
.entity("Widget " + id + " not found.").build());
} catch(Exception e) {
throw new WebApplicationException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("I don't know what happened, but trying again won't help: "
+ e.getMessage())
.build());

}

return w;
}

注意:只有 Response 返回给客户端,除非你定义一个自定义的 ExceptionMapper

注意:如果您独立处理特定的异常,而不是捕获 Throwable,您的代码将更具可读性。上面,我已将每个 Java 异常映射到一般的 Jersey 内部服务器错误。

关于java - REST 错误响应和客户端-服务器 POJO 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26595887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com