我正在尝试在我的 GWT 应用程序中使用 Errai 休息功能,
我查看了以下指南:
http://errai-blog.blogspot.it/2011/10/jax-rs-in-gwt-with-errai.html
特别是,它说:
We simply put this interface somewhere in our client packages (e.g. client.shared) where the GWT compiler can find it. To create a request all that needs to be done is to invoke RestClient.create()
我认为这里有一个情节漏洞,Errai 如何知道如何序列化/反序列化模型类?
你能帮忙理解一下吗?
谢谢
最佳答案
根据RestClient
类的create()方法
public static <T, R> T create(final Class<T> remoteService, final RemoteCallback<R> callback, Integer... successCodes) {
return create(remoteService, null, callback, null, successCodes);
}
在您提供的示例中;当使用 create() 方法时,Errai 将 CustomerService 类获取为 remoteService
,经过多次操作;
Errai 使用其 errai-codegen 解析并实现此 CustomerService 接口(interface)使用 Java Reflection API 的库.
简单解析时;
首先它查找 JAX-RS 带注释的方法并将它们定义为 JaxrsResourceMethod
然后,如果有任何用 JAX-RS 注释进行注释的参数,它会检查该方法的参数。
如果在 JaxrsResourceMethod 中找到带注释的参数,它保留该参数及其注释类型
如果在 JaxrsResourceMethod 中发现没有带注释的参数它定义为 entityParameter
Errai 将这些带注释的参数和实体参数保存在JaxrsResourceMethodParameters中。通过他们的方法。构建请求时,它按照规则使用参数。
让我使用您提供的示例来解释这些规则。
Customer customer = new Customer("new name", "new last name", "new postal code");
RestClient.create(CustomerService.class, callback).updateCustomer(240193, customer);
Errai 将创建类似
的网址example.com/cusomers/240193
因为@PathParam("id")注释规则是在url中添加参数,并且根据Errai的entityParameter规则,当使用PUT发送数据时,customer
将被编码。
@PUT
@Path("/{id}")
@Consumes("application/json")
@Produces("application/json")
public Customer updateCustomer(@PathParam("id") long id, Customer customer); //- See more at: http://errai-blog.blogspot.com.tr/2011/10/jax-rs-in-gwt-with-errai.html#sthash.2GTQtIg8.dpuf
如果您检查here,还有一个附加说明setEntityParameter方法出现异常;
Only one non-annotated entity parameter allowed per method:
这意味着您无法在 Errai 中发送的类中定义具有超过 1 个非注释参数的方法。
关于java - Errai 如何序列化/反序列化模型实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37379288/