0

我正在尝试使用RequestFactory集成GWTHibernate。一切正常,除非我尝试使用现有实体。不管我要做什么(加载、更新或删除),结果总是一样的: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="
    }]
}
4

2 回答 2

0

“O”变量是通过线路发送的操作,在com.google.web.bindery.requestfactory.shared.messages.RequestMessage. 操作描述在com.google.web.bindery.requestfactory.shared.messages.OperationMessage- 它表示在实体持久化之前要在实体上执行的一个或多个操作(与com.google.web.bindery.requestfactory.shared.messages.InvocationMessage表示服务操作相反)。

对于在那里出现的空指针,整个 RequestMessage 似乎需要为空 - 没有对象可以从中读取“O”属性 - JsonSplittable 是在没有基础数据的情况下以某种方式创建的。

我没有看到一种方法可以跟踪调用以将消息从客户端反序列化到 RequestMessage 实例的代码,但有一个例外 - 如果发送的是 JSON 字符串而不是对象。这没有任何意义,至少在任何默认设置下都是如此。


从更新的帖子中可以看出,JsonSplittable 是用字符串而不是 Json 对象创建的,这为我们提供了 NPE。由此,我们只知道一些字符串正在被反序列化,但服务器认为它应该是一个对象,因此正在尝试(并且失败)读取子属性。现在,仅从堆栈跟踪就证明我们正在读取的属性是困难的,因为它可能正在读取任何子属性以准备将其作为 bean 访问。id 属性是一个可能的候选对象,因为它应该作为 String->Long 发送,但如果服务器期待其他类型,它会尝试将其反序列化为 jsonobject->object。

其他不向服务器发送对象的操作似乎可以证实这一点。我仍然怀疑这里出了什么问题(或者堆栈跟踪已经改变,或者它被卡住的属性已经改变) - 弄清楚它当前试图读取哪个属性对于解决这个问题很重要。

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)

这里的最后一帧是实际调用 getter 的地方,尝试打破异常以读取一些值并学习一些东西:

  • 这是否仍然从 getOperations 发生,并且在字符串上调用的属性仍然是“O”吗?假设如此,
  • JsonSplittable 中被视为对象的字符串是什么?对于请求字符串,除了 O 数组(或“UPDATE”字符串->枚举)中唯一的对象之外,这没有任何意义。

我问的其他一些问题得到了回答

您可以发布通过网络发送的消息正文吗?这应该在堆栈跟踪中可用jsonRequestStringcom.google.web.bindery.requestfactory.server.RequestFactoryServlet.doPost(HttpServletRequest, HttpServletResponse)或者使用 Firebug 或 Chrome Inspector 等工具查看文本。您还可以分享您如何创建/设置 RequestFactory 实例吗?如果您使用的是自定义传输,则数据可能以某种方式发送错误(尽管看起来很荒谬)。

最后,任何射频呼叫都有效吗?如果它们都失败了,这也表明存在某种配置问题。如果这是唯一的一个,那么也许该消息会稍微阐明这个问题。

于 2012-02-28T20:51:32.160 回答
0

我不确定您的代理中是否需要 stableId() 方法。我认为这是在客户端为您生成的。不确定这是您的问题,但在您的代理界面中没有它值得一试。

于 2012-02-28T16:17:27.450 回答