我有以下 Java servlet,它执行我所谓的“添加服务”:
public class AdditionService extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
// The request will have 2 Integers inside its body that need to be
// added together and returned in the response.
Integer addend = extractAddendFromRequest(request);
Integer augend = extractAugendFromRequest(request);
Integer sum = addend + augend;
PrintWriter writer = response.getWriter();
writer.write(sum);
}
}
我正在尝试使用 and 让 GWT 的 RequestFactory 做同样的事情(在应用服务器上添加两个数字并返回总和作为响应)ValueProxy
,AdditionService
并且遇到了一些问题。
这是AdditionRequest
(客户端层),它是一个包含两个要添加的整数的值对象:
// Please note the "tier" (client, shared, server) I have placed all of my Java classes in
// as you read through the code.
public class com.myapp.client.AdditionRequest {
private Integer addend;
private Integer augend;
public AdditionRequest() {
super();
this.addend = 0;
this.augend = 0;
}
// Getters & setters for addend/augend.
}
接下来我的代理(客户端层):
@ProxyFor(value=AdditionRequest.class)
public interface com.myapp.client.AdditionRequestProxy extends ValueProxy {
public Integer getAddend();
public Integer getAugend();
public void setAddend(Integer a);
public void setAugend(Integer a);
}
接下来我的服务 API(在共享层中):
@Service(value=DefaultAdditionService.class)
public interface com.myapp.shared.AdditionService extends RequestContext {
Request<Integer> sum(AdditionRequest request);
}
接下来我的请求工厂(共享层):
public class com.myapp.shared.ServiceProvider implements RequestFactory {
public AdditionService getAdditionService() {
return new DefaultAdditionService();
}
// ... but since I'm implementing RequestFactory, there's about a dozen
// other methods GWT is forcing me to implement: find, getEventBus, fire, etc.
// Do I really need to implement all these?
}
最后是魔法发生的地方(服务器层):
public class com.myapp.server.DefaultAdditionService implements AdditionService {
@Override
public Request<Integer> sum(AdditionRequest request) {
Integer sum = request.getAddend() + request.getAugend();
return sum;
}
// And because AdditionService extends RequestContext there's another bunch of
// methods GWT is forcing me to implement here: append, create, isChanged, etc.
// Do I really need to implement all these?
}
以下是我的问题:
- 我的“层”策略正确吗?我是否将所有类型打包在正确的客户端/共享/服务器包中?
- 我认为我的设置不正确,因为
AdditionService
(in shared) referencesDefaultAdditionService
位于服务器上,它不应该这样做。共享类型应该能够同时存在于客户端和服务器上,但不依赖于任何一个......
- 我认为我的设置不正确,因为
- 应该
ServiceProvider
是实现的类RequestFactory
,还是扩展它的接口?如果是后者,我在哪里定义ServiceProvider
impl,以及如何将它链接回所有这些其他类? ServiceProvider
和中的所有这些方法DefaultAdditionService
呢?我是否需要实现所有 20 多种核心 GWT 方法?或者我是否错误地使用了 API,或者没有尽可能简单地使用它?- 服务定位器在哪里起作用?如何?