RequestFactory:代理实现与泛型的接口

时间:2014-07-10 09:21:20

标签: java generics gwt requestfactory

我正在努力处理requestfactories和泛型。 在下面的代码中,代理方法与实体方法完全匹配,但我得到了日志跟踪:

java.lang.NullPointerException: null
    at com.google.web.bindery.autobean.vm.impl.MethodPropertyContext.traverse(MethodPropertyContext.java:102) ~[gwt-servlet-2.6.0.jar:na]
    at com.google.web.bindery.autobean.vm.impl.MethodPropertyContext.accept(MethodPropertyContext.java:75) ~[gwt-servlet-2.6.0.jar:na]
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyCoderCreator.maybeCreateCoder(AutoBeanCodexImpl.java:353) ~[gwt-servlet-2.6.0.jar:na]
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyCoderCreator.visitReferenceProperty(AutoBeanCodexImpl.java:341) ~[gwt-servlet-2.6.0.jar:na]
    at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.traverseProperties(ProxyAutoBean.java:324) ~[gwt-servlet-2.6.0.jar:na]
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.traverse(AbstractAutoBean.java:166) ~[gwt-servlet-2.6.0.jar:na]
...

我的代理类:

@ProxyFor(value = OutilLibre.class, locator = OutilLibreLocator.class)
public interface OutilLibreProxy extends ProxyWithId, ProxyWithCartoLibre<OutilProxy>, EntityProxy {

    public Long getId();

    public void setId(Long id);

    @Override
    OutilProxy getCompetence();

    @Override
    void setCompetence(OutilProxy outil);

    @Override
    String getCompetenceAutre();

    @Override
    void setCompetenceAutre(String competence);

}

带有泛型的界面:

public interface ProxyWithCartoLibre<T> {

    T getCompetence();

    void setCompetence(T competence);

    String getCompetenceAutre();

    void setCompetenceAutre(String competence);

}   

相应的服务器实体:

@Entity
@Table(name = "outil_libre")
public class OutilLibre extends AEntityLongId implements IBeanCartoLibre<Outil>, IUniqueVersionEntity {

    private static final long serialVersionUID = 45107725880220830L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "outil_libre_seq_gen")
    @SequenceGenerator(name = "outil_libre_seq_gen", sequenceName = "seq_outil_libre")
    @Column(name = "id", unique = true, nullable = false, precision = 5, scale = 0)
    private Long id;

    @Cascade(value = {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "outil", nullable = false)
    private Outil outil;

    @Column(name = "outil_autre")
    private String outilAutre;

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public Outil getCompetence() {
        return outil;
    }

    @Override
    public void setCompetence(Outil outil) {
        this.outil = outil;
    }

    @Override
    public String getCompetenceAutre() {
        return outilAutre;
    }

    @Override
    public void setCompetenceAutre(String competence) {
        this.outilAutre = competence;
    }

}   

我不知道为什么会出现此错误,因为ProxyWithCartoLibre接口仅由客户端类使用,并且没有任何用于定义requestfactory的用途。

有没有办法告诉GWT生成器排除ProxyWithCartoLibre接口?

提前致谢。

修改

java.lang.NullPointerException: null
     at com.google.web.bindery.autobean.vm.impl.MethodPropertyContext.traverse(MethodPropertyContext.java:102) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.vm.impl.MethodPropertyContext.accept(MethodPropertyContext.java:75) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyCoderCreator.maybeCreateCoder(AutoBeanCodexImpl.java:353) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl$PropertyCoderCreator.visitReferenceProperty(AutoBeanCodexImpl.java:341) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.traverseProperties(ProxyAutoBean.java:324) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.traverse(AbstractAutoBean.java:166) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.accept(AbstractAutoBean.java:101) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl.doCoderFor(AutoBeanCodexImpl.java:522) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.setProperty(AbstractAutoBean.java:276) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.setProperty(ProxyAutoBean.java:253) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.vm.impl.BeanPropertyContext.set(BeanPropertyContext.java:44) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.requestfactory.server.Resolver$PropertyResolver.visitValueProperty(Resolver.java:211) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.traverseProperties(ProxyAutoBean.java:289) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.traverse(AbstractAutoBean.java:166) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.accept(AbstractAutoBean.java:101) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.requestfactory.server.Resolver.resolveClientValue(Resolver.java:470) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.processInvocationMessages(SimpleRequestProcessor.java:491) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.process(SimpleRequestProcessor.java:233) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.requestfactory.server.SimpleRequestProcessor.process(SimpleRequestProcessor.java:135) ~[gwt-servlet.jar:na]
     at com.google.web.bindery.requestfactory.server.RequestFactoryServlet.doPost(RequestFactoryServlet.java:133) ~[gwt-servlet.jar:na]
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) [servlet-api.jar:na]
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:na]
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.39.B]
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.39.B]
     at ch.qos.logback.access.servlet.TeeFilter.doFilter(TeeFilter.java:55) [logback-access-1.0.13.jar:na]
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.39.B]
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.39.B]
     at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180) [spring-orm-3.2.4.RELEASE.jar:3.2.4.RELEASE]
     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.39.B]
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.39.B]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
     at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
     at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.39.B]
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.39.B]
     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.39.B]
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.39.B]
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.39.B]
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.39.B]
     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.39.B]
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.39.B]
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.39.B]
     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947) [catalina.jar:7.0.39.B]
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.39.B]
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [catalina.jar:7.0.39.B]
     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009) [tomcat-coyote.jar:7.0.39.B]
     at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) [tomcat-coyote.jar:7.0.39.B]
     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) [tomcat-coyote.jar:7.0.39.B]
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_60]
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_60]
     at java.lang.Thread.run(Thread.java:745) [na:1.7.0_60]

1 个答案:

答案 0 :(得分:0)

我也遇到了这个例外并尝试了很多,并提出了以下解决方法:

  1. 建立一个基础&#34;底层&#34;你的班级的bean / POJO模型避免了泛型(在最外层,使用泛型似乎不是问题)并且可能使用一些&#34;标记字符串&#34;像_一样帮助避免(通过&#34;乐观&#34; 接口用法)以后使用&#34;不受欢迎的类/方法&#34;或提供所需的变体,例如:

    interface _Person< PERSON extends _Person<PERSON> > extends IsSerializable {  
                       // generics here: just to show that generics work if _Person is not 
                       // referenced itself in AutoBean (de)serialization
      int getId();
      void setId( int id );     // normally we may not want to expose this, but for the
                                // (de)serialization it is/may be necessary or benefitial 
                                // here now
      String getName();
      void setName( String name );
      //_Person setName_( String name );
                            // to have fluent setters is against Bean definition and does 
                            // not work with AutoBeans, but since there is no getter
                            // "getName()" it is ignored and would work :-)
                            // (but it may be better to not include it in here)
      _Address getAddr_();
      void setAddr_( _Address addr );
    }
    
    //interface _Address< ADDR extends _Address > extends IsSerializable {
      // => would e.g. cause the NullPointerException ! :-(
      //    at com.google.web.bindery.autobean.vm.impl.MethodPropertyContext.traverse(
      //         MethodPropertyContext.java:102) ~[gwt-user-2.6.0.jar:na]
    interface _Address extends IsSerializable {
       String getCity_();
       void setCity_( String city );
    }
    
    • 现在使用 AutoBean 功能可以使用它们,如果有人使用通用接口,后面的实现应该可以工作(我只尝试了一个这样的简单例子来测试它)。 上面的示例应该有效(不完整的代码段):AutoBeanCodex.encode( personAutoBean ).deepCopy()
  2. 现在创建通用接口和从这些接口继承的类将具有例如缺点无法

    • 返回通用person.getAddr_().getNonBeanFooExtension()
    • 使用流畅的界面,例如person.setName_( "foo" ).setMail( "bar" )
    • 阻止/阻止访问person.setId( 123 )

    因此,我现在正在计划(我将在完成后报告)从这些来源中得出,但写作&#34;继承无关&#34; /重叠接口,通常&#34;通常&#34;在整个商业应用程序中使用,例如:

    interface Person< PERSON extends Person<PERSON>> {
       // extends _Person <= no, because then we could not "hide/discourage" users calling 
       // e.g. "setId(....)"
      int getId();
    
      String getName();
      //void setName( String name );  // we could provide it, but setName_() does the same
                                      // and more and only one setter keeps the interface 
                                      // smaller
      PERSON setName_( String name );
      Address<PERSON> getAddr();
      PERSON setAddr( Address<PERSON> addr );
    
      PERSON setFancyThing();          // not "seen" by AutoBean and thus should not 
                                       // cause trouble there
    }
    
    interface Address< PERSON extends Person<PERSON> > {
      ...
    }
    
  3. 现在实现这样的类应该可以正常工作(现在只测试简单的情况):

    class PersonDTO implements Person<PersonDTO>, _Person<PersonDTO> {
      ...
    }
    
  4. &#34;用户&#34;这些实现应该尽可能地使用2. Person<PersonDto> pers = ... 的接口,这样他们就会看到一些非常好的人机界面,而不能轻松调用 pers.setId( 123 ),但pers.setName_( "Dimitri Khalezov" ).setMail_( ... )

  5. 这已经是一篇很长的帖子了,我希望你能想象各个地方缺少的细节。