带有注入服务的 REST 端点 - 在哪里抛出自定义 WebServiceException?
REST endpoint with injected services - where to throw custom WebServiceException?
我有注入服务的 REST 端点。
@Singleton
@Path("/v1/service")
public class MyService {
@Inject
private TokenService tokenService;
@Inject
private InfoService infoService;
@GET
@Path("/info")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public InfoResponse getInfo(@HeaderParam("Authorization") String token){
tokenService.validateToken(token);
List<String> info = infoService.getInfo();
// processing info code, throwing WebServiceException if something wrong
return new InfoResponse(info);
}
}
其中一些验证数据,例如令牌,如果不正确则抛出 RestServiceException 异常。异常由 ExceptionMapper 处理,因此返回适当的响应。其他服务 return/process 信息。
我的自定义异常 class 具有附加字段:
public class RestServiceException extends WebServiceException {
public Integer httpStatus;
public String message;
}
示例服务方法:
public User getUser(Long userId){
User user = userMapper.getUser(userId);
if (user == null){
String msg = "Invalid user: " + userId;
logger.debug(msg);
throw new RestServiceException(HttpStatus.SC_NOT_FOUND, InternalStatus.INVALID_USER, msg);
}
return user;
}
在注入的服务中抛出 RestServiceException 是好的做法吗?如何设计代码以方便测试?在哪里放置验证方法 - 服务或静态 class?
Is good practice to throw WebServiceException in injected services?
我不会。
- 使服务在 jax-rs 应用程序之外不可重用(对您来说可能不是什么大问题)。
打破了"tiered"架构规则;下层不应该知道上层的任何信息。你应该只抛出一个正常的服务级别异常,如果你想包装在 WebApplicationException
中,然后从那里捕获资源中的异常,然后这样做。
也许只是抛出 WebApplicationException
不被考虑 "knowing about the upper layer",但我个人认为是这样,因为异常类型特定于上层。
How to design code to facilitate testing?
使用构造函数注入而不是字段注入。如果需要,可以更轻松地为资源提供模拟服务。
public class MyService {
private final TokenService tokenService;
private final InfoService infoService;
@Inject
public MyService(TokenService tokenService,
InfoService infoService) {
this.tokenService = tokenService;
this.infoService = infoService;
}
}
Where to put validation methods - service or static class?
这真的没有意义。如果 "static class" 你的意思是单例模式单例,那么绝对不是。按照您当前的方式使用控制反转是您更好的选择。
更新
我想我读错了你的问题。出于某种原因,我将您的 WebServiceException
读为 JAX-RS WebApplicationException
。
我想我上面提到的规则不适用。但我可能会将异常的名称更改为 WebServiceException
以外的名称。这与上层 "web" 有很大关系。为该服务特定的异常命名。
关于在资源级别处理异常,如果您不想,则不需要对它做任何事情,如果 ExceptionMapper
正在处理它。或者您可以根据需要捕获它并将其包装在 WebApplicationException
.
中
您可以查看 this post 的底部,其中列出了 WebApplicationException
的所有子 类。如果你愿意,你可以将异常包装在其中一个中并抛出它。这些异常都映射到状态代码。
我有注入服务的 REST 端点。
@Singleton
@Path("/v1/service")
public class MyService {
@Inject
private TokenService tokenService;
@Inject
private InfoService infoService;
@GET
@Path("/info")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public InfoResponse getInfo(@HeaderParam("Authorization") String token){
tokenService.validateToken(token);
List<String> info = infoService.getInfo();
// processing info code, throwing WebServiceException if something wrong
return new InfoResponse(info);
}
}
其中一些验证数据,例如令牌,如果不正确则抛出 RestServiceException 异常。异常由 ExceptionMapper 处理,因此返回适当的响应。其他服务 return/process 信息。
我的自定义异常 class 具有附加字段:
public class RestServiceException extends WebServiceException {
public Integer httpStatus;
public String message;
}
示例服务方法:
public User getUser(Long userId){
User user = userMapper.getUser(userId);
if (user == null){
String msg = "Invalid user: " + userId;
logger.debug(msg);
throw new RestServiceException(HttpStatus.SC_NOT_FOUND, InternalStatus.INVALID_USER, msg);
}
return user;
}
在注入的服务中抛出 RestServiceException 是好的做法吗?如何设计代码以方便测试?在哪里放置验证方法 - 服务或静态 class?
Is good practice to throw WebServiceException in injected services?
我不会。
- 使服务在 jax-rs 应用程序之外不可重用(对您来说可能不是什么大问题)。
打破了"tiered"架构规则;下层不应该知道上层的任何信息。你应该只抛出一个正常的服务级别异常,如果你想包装在
WebApplicationException
中,然后从那里捕获资源中的异常,然后这样做。也许只是抛出
WebApplicationException
不被考虑 "knowing about the upper layer",但我个人认为是这样,因为异常类型特定于上层。
How to design code to facilitate testing?
使用构造函数注入而不是字段注入。如果需要,可以更轻松地为资源提供模拟服务。
public class MyService {
private final TokenService tokenService;
private final InfoService infoService;
@Inject
public MyService(TokenService tokenService,
InfoService infoService) {
this.tokenService = tokenService;
this.infoService = infoService;
}
}
Where to put validation methods - service or static class?
这真的没有意义。如果 "static class" 你的意思是单例模式单例,那么绝对不是。按照您当前的方式使用控制反转是您更好的选择。
更新
我想我读错了你的问题。出于某种原因,我将您的 WebServiceException
读为 JAX-RS WebApplicationException
。
我想我上面提到的规则不适用。但我可能会将异常的名称更改为 WebServiceException
以外的名称。这与上层 "web" 有很大关系。为该服务特定的异常命名。
关于在资源级别处理异常,如果您不想,则不需要对它做任何事情,如果 ExceptionMapper
正在处理它。或者您可以根据需要捕获它并将其包装在 WebApplicationException
.
您可以查看 this post 的底部,其中列出了 WebApplicationException
的所有子 类。如果你愿意,你可以将异常包装在其中一个中并抛出它。这些异常都映射到状态代码。