JAX-RS 响应的实用程序、组合与继承
我正在考虑编写一个创建并返回 JAX-RS 响应的实用程序类。目标是简化和标准化响应的生成。想法是这样的:
public Object success (Class targetClass, String methodName, Object responseEntity) {
URI location = UriBuilder.fromResource(targetClass).path(targetClass, methodName).build(localVar1, localVar2);
LogInfo(location, methodName); //log4J stuff
return Response.created(location).entity(responseEntity).build();
}
对于其他响应类型(例如取消、错误等),还会有其他方法。
我很好奇其他人如何解决这个设计问题,也许使用继承或组合。
过去要如何解决这个问题?您是否也创建了一个实用程序类,或者您是否使用继承或组合来设计解决方案?你为什么选择这个设计?
I am thinking about writing a utility class that creates and returns a JAX-RS Response. The goal is to simplify and standardize the generation of Response's. Here's the idea:
public Object success (Class targetClass, String methodName, Object responseEntity) {
URI location = UriBuilder.fromResource(targetClass).path(targetClass, methodName).build(localVar1, localVar2);
LogInfo(location, methodName); //log4J stuff
return Response.created(location).entity(responseEntity).build();
}
And there would be additional methods for other response types (e.g. canceled, error, etc).
I was curious about how someone else may solve this design problem, perhaps using inheritance or composition.
How have to solved this in the past? Did you create a single utility class as well, or did you use inheritance or composition to design the solution? Why did you go with that design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,这看起来像是一个低级实用程序。我说不要想太多 - 创建某种定制的响应继承层次结构和/或新的组合类型系列(为了添加“模式”)感觉没有必要。
我将创建一个简单的响应实用程序,为公共部分提取一个私有方法,并在(且仅当)您的需求变得更加复杂时重新考虑这个简单的设计。
This looks like a low-level utility to me. I say don't over think it - creating some kind of bespoke response inheritance hierarchy and or new family of composed types (for the sake of added 'patterniness') feels unnecessary.
I'd create a simple response utility, extract a private method for the commons parts, and rethink this simple design if (and only if) your requirements become more complex.