我正在使用Google Guava库执行Java EE 7应用程序中java.util.List<T>
的排序和过滤等任务。
下面给出了根据CDI bean中的过滤器列表过滤java.util.List<T>
的示例。
@Named
@ViewScoped
public class Bean extends LazyDataModel<T> implements Serializable {
private static final long serialVersionUID = 1L;
private final class Filtering implements Predicate<T> {
private final Map<String, Object> filters;
public Filtering(Map<String, Object> filters) {
this.filters = filters;
}
@Override
public boolean apply(T p) {
if (p == null) {
return false;
}
Integer id = (Integer) filters.get("id");
if (id != null && !p.getId().equals(id)) {
return false;
}
BigDecimal size = (BigDecimal) filters.get("size");
if (size != null && ((p.getSize().compareTo(size) < 0))) {
return false;
}
return true;
}
}
}
嵌套类可用于从CDI bean中过滤java.util.List<T>
,如下所示。
List<T> list = //Initialize the list here to be filtered.
//...
list = FluentIterable.from(list).filter((new Filtering(filters))).toList();
其中filters
是java.util.Map<String, Object>
,其中包含过滤器列表。
以前在带有 JSF Managed Beans 的Java EE应用程序中(在GlassFish 4.0上)工作正常。
我使用 CDI Managed Beans 将此应用程序迁移到GlassFish 4.1,并发生以下异常。
SEVERE: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.toList()Lcom/google/common/collect/ImmutableList;
at util.mesurements.custom.beans.BackNeckDepthManagedBean.load(BackNeckDepthManagedBean.java:184)
at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:838)
at org.primefaces.component.datatable.feature.FilterFeature.encode(FilterFeature.java:102)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:78)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:582)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at org.primefaces.component.api.UIData.visitTree(UIData.java:692)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIForm.visitTree(UIForm.java:371)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:322)
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:219)
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:430)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:72)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at filter.NoCacheFilter.doFilter(NoCacheFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:722)
我在两个应用程序上都使用了Guava 17.0(仔细检查过它)。
为什么Guava不喜欢CDI bean。可能是什么解决方案/解决方法?
更新:
我尝试将版本更改为18.0但没有任何新的发生。
答案 0 :(得分:11)
您的类路径上有多个版本的Guava。在一个番石榴类中NoSuchMethodError
来到这里可能每周一次。只要你没有摆脱旧版本,切换到新版本就不会有帮助。打印FluentIterable.class
的位置并删除该JAR。
正如Frank Pavageau评论的那样,这个位置可以作为
获得FluentIterable.class.getProtectionDomain().getCodeSource().getLocation().toExternalForm()
答案 1 :(得分:5)
这是一个已知的GlassFish问题。使用GlassFish 4.1并且没有其他修改,使用了Guava 13.0.1,因此当您使用该版本中不存在的功能时,您将收到类似于您所声明的错误。就我而言,它是java.lang.IncompatibleClassChangeError: Implementing class
。
在bug report的评论中,建议替换glassfish/modules/guava.jar
。这似乎是最简单的解决方案。另一种方法是使用自定义类加载器。
修改:如果您向<class-loader delegate="false"/>
添加glassfish-web.xml
,则会从WAR加载Guava。
答案 2 :(得分:1)
我遇到了错误 java.lang.NoSuchMethodError:com.google.common.collect.FluentIterable.append(Ljava / lang / Iterable;)Lcom / google / common / collect / FluentIterable; 从16.0.1切换到番石榴20.0版,为我解决了这个问题。