我正在尝试使用RequestFactory集成GWT和Hibernate。一切正常,除非我尝试使用现有实体。不管我要做什么(加载、更新或删除),结果总是一样的:NullPointerException。
经过一些调试,我意识到当请求被触发时,一个只包含一个字符串的JsonSplittable包含我现有实体的 ID(例如“1”),就像它有一个JSONObject一样被处理。方法getOrReify被执行并且 NPE 发生。
getOrReify似乎正在寻找要执行的操作,因为它正在搜索的是“O”(propertyName )。但是,正如我所说,它不会找到它,因为没有 JSONObject。另外,可以提供此信息的reifiedMap没有它(但它不为空)。
例外
28/02/2012 11:10:51 com.google.web.bindery.requestfactory.server.RequestFactoryServlet doPost
GRAVE: Unexpected error
java.lang.NullPointerException
at com.google.web.bindery.autobean.vm.impl.JsonSplittable.isNull(JsonSplittable.java:248)
at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.getOrReify(AbstractAutoBean.java:235)
at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.getOrReify(ProxyAutoBean.java:229)
at com.google.web.bindery.autobean.vm.impl.BeanMethod$2.invoke(BeanMethod.java:73)
at com.google.web.bindery.autobean.vm.impl.SimpleBeanHandler.invoke(SimpleBeanHandler.java:43)
at $Proxy81.getOperations(Unknown Source)
at sun.reflect.GeneratedMethodAccessor67.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.web.bindery.autobean.vm.impl.ShimHandler.invoke(ShimHandler.java:78)
at $Proxy81.getOperations(Unknown Source)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.processOperationMessages(SimpleRequestProcessor.java:496)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.decodeOobMessage(SimpleRequestProcessor.java:185)
at com.google.web.bindery.requestfactory.server.RequestState.getBeansForIds(RequestState.java:254)
at com.google.web.bindery.requestfactory.server.RequestState.getBeansForPayload(RequestState.java:147)
at com.google.web.bindery.requestfactory.server.RequestState.getBeanForPayload(RequestState.java:124)
at com.google.web.bindery.requestfactory.shared.impl.EntityCodex.decode(EntityCodex.java:101)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.decodeInvocationArguments(SimpleRequestProcessor.java:409)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.decodeInvocationArguments(SimpleRequestProcessor.java:380)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.processInvocationMessages(SimpleRequestProcessor.java:447)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.process(SimpleRequestProcessor.java:225)
at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.process(SimpleRequestProcessor.java:127)
at com.google.web.bindery.requestfactory.server.RequestFactoryServlet.doPost(RequestFactoryServlet.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)
at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178)
at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62)
at my.package.persistence.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:159)
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163)
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58)
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:168)
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58)
at my.package.persistence.filter.TenantFilter.doFilter(TenantFilter.java:171)
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163)
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58)
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:168)
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58)
at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1088)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
实体
@Entity
public class Foro implements RequestFactoryEntity<Long> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(length = 100)
private String descricao;
@Column(name = Constants.VERSION_COLUMN)
@Version
private Integer version;
public Foro() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
}
代理人
@ProxyFor(value = Foro.class, locator = Locators.Foro.class)
public interface ForoProxy extends EntityProxy {
EntityProxyId<ForoProxy> stableId();
String getDescricao();
void setDescricao(String descricao);
}
定位器(用超类简化)
// Locator
public final class Locators {
private Locators() {
}
public static final class Foro extends AbstractDaoLocator<ForoDao, my.package.evolution.core.entity.Foro, Long> {
}
}
// Locator superclass
public abstract class AbstractDaoLocator<DAO extends AbstractDao<T, ID>, T extends RequestFactoryEntity<ID>, ID extends Serializable> extends AbstractLocator<T, ID> {
@Override
public T find(Class<? extends T> clazz, ID id) {
try {
return getDaoClass().newInstance().findById(id);
} catch (Exception e) {
throw new RuntimeException("Falha ao instanciar DAO: " + getDaoClass().getName() + ". Este DAO tem um construtor vazio?", e);
}
}
@Override
@SuppressWarnings("unchecked")
public Class<T> getDomainType() {
return (Class<T>) getTypes()[1];
}
@Override
@SuppressWarnings("unchecked")
public Class<ID> getIdType() {
return (Class<ID>) getTypes()[2];
}
@SuppressWarnings("unchecked")
private Class<DAO> getDaoClass() {
return (Class<DAO>) getTypes()[0];
}
private Type[] getTypes() {
return ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
}
}
// AbstractDaoLocator superclass
public abstract class AbstractLocator<T extends RequestFactoryEntity<ID>, ID extends Serializable> extends Locator<T, ID> {
@Override
public T create(Class<? extends T> clazz) {
try {
return clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
@SuppressWarnings("unchecked")
public Class<T> getDomainType() {
return (Class<T>) getTypes()[0];
}
@Override
public ID getId(T domainObject) {
return domainObject.getId();
}
@Override
@SuppressWarnings("unchecked")
public Class<ID> getIdType() {
return (Class<ID>) getTypes()[1];
}
@Override
public Object getVersion(T domainObject) {
return domainObject.getVersion();
}
private Type[] getTypes() {
return ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
}
}
ServiceLocator(使用超类简化)
// ServiceLocator
public final class ServiceLocators {
private ServiceLocators() {
}
public static final class ForoDao extends AbstractServiceLocator<br.com.programarte.evolution.core.dao.ForoDao> {
}
}
// ServiceLocator superclass
public abstract class AbstractServiceLocator<T> implements ServiceLocator {
public Object getInstance(Class<?> clazz) {
try {
return getServiceClass().newInstance();
} catch (Exception e) {
throw new RuntimeException("Falha ao instanciar a classe de servi\u00e7o: " + getServiceClass().getName() + ". Ela tem um construtor vazio?", e);
}
}
@SuppressWarnings("unchecked")
private Class<T> getServiceClass() {
return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
}
DAO(Hibernate 书中的超类 AbstractDao)
public class ForoDao extends AbstractDao<Foro, Long>{
}
请求工厂
public interface RequestContextFactory extends RequestFactory {
ForoRequestContext foroRequestContext();
@Service(value = ForoDao.class, locator = ServiceLocators.ForoDao.class)
public interface ForoRequestContext extends RequestContext {
Request<ForoProxy> findById(Long id);
Request<ForoProxy> saveOrUpdate(ForoProxy proxy);
Request<Void> delete(ForoProxy proxy);
}
执行
getFactory().foroRequestContext().findById(3L).fire(new SimpleReceiver<ForoProxy>() {
@Override
public void onSuccess(ForoProxy entity) {
final ForoRequestContext ctx = getFactory().foroRequestContext();
final ForoProxy entityEdit = ctx.edit(entity);
entityEdit.setDescricao(entityEdit.getDescricao() + " (Edit)");
ctx.saveOrUpdate(entityEdit).fire(new SimpleReceiver<ForoProxy>());
}
});
更多信息(由 Colin Alworth 要求)
创建/设置请求工厂
private RequestContextFactory rcf;
private RequestContextFactory getFactory() {
if (null == rcf) {
rcf = new SmartContextFactory().getRequestContextFactory();
}
return rcf;
}
智能上下文工厂###
public class SmartContextFactory {
private final RequestContextFactory rcf = GWT.create(RequestContextFactory.class);
private final EventBus eventBus = new SimpleEventBus();
public SmartContextFactory() {
rcf.initialize(eventBus);
}
public RequestContextFactory getRequestContextFactory() {
return rcf;
}
public EventBus getEventBus() {
return eventBus;
}
}
jsonRequestString(美化)
{
"F": "my.package.evolution.services.requestfactory.RequestContextFactory",
"O": [{
"T": "cv432aQBAMWN$1T23hbccUQX5WY=",
"V": "MS4w",
"P": {
"descricao": "Nowhere (1330517239646) (Edit)"
},
"S": "IjI3Ig==",
"O": "UPDATE"
}],
"I": [{
"P": [{
"T": "cv432aQBAMWN$1T23hbccUQX5WY=",
"S": "IjI3Ig=="
}],
"O": "r6H_7BrJ_7Y_j0lWkuJA$zEJ6mc="
}]
}