Spring Boot + Primefaces - “无法识别的内容类型”异常
Posted
技术标签:
【中文标题】Spring Boot + Primefaces - “无法识别的内容类型”异常【英文标题】:Spring Boot + Primefaces - "Unrecognized Content Type" Exception 【发布时间】:2018-11-11 09:06:39 【问题描述】:我有一个使用 spring boot 和 joinfaces 的基本设置,以便在我的应用程序中使用 primefaces 和 bootsfaces。
使用例如 bootsfaces 组件并执行 ajax 调用就像一个魅力。 当我添加一个使用 ajax 的简单 primefaces 组件时,调用失败并出现以下异常:
2018-06-01 11:49:22.479 ERROR 11868 --- [nio-8080-exec-2] j.e.r.webcontainer.jsf.application : Error Rendering View[/datatest-prime.xhtml]
java.lang.IllegalArgumentException: Unrecognized Content Type.
at com.sun.faces.renderkit.RenderKitImpl.createResponseWriter(RenderKitImpl.java:283) ~[javax.faces-2.3.4.jar:2.3.4]
at com.sun.faces.application.view.FaceletViewHandlingStrategy.createResponseWriter(FaceletViewHandlingStrategy.java:1160) ~[javax.faces-2.3.4.jar:2.3.4]
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:438) ~[javax.faces-2.3.4.jar:2.3.4]
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:194) [javax.faces-2.3.4.jar:2.3.4]
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:151) [javax.faces-2.3.4.jar:2.3.4]
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:126) [javax.faces-2.3.4.jar:2.3.4]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100) [javax.faces-2.3.4.jar:2.3.4]
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:223) [javax.faces-2.3.4.jar:2.3.4]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:671) [javax.faces-2.3.4.jar:2.3.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.28.jar:8.5.28]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) [spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) [spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.28.jar:8.5.28]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_162]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_162]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.28.jar:8.5.28]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_162]
如果我只使用本例中的按钮也没关系:
<h:form>
<p:commandButton value="Add" ajax="true" actionListener="#pingResultBean.onAction" />
</h:form>
或使用任何其他触发 ajax 调用的 primefaces 组件。
我已启用 Spring 安全性并使用以下配置:
http
.authorizeRequests()
.antMatchers("/login*").permitAll()
.antMatchers("/logout*").permitAll()
.antMatchers("/js/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/javax.faces.resource/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login.xhtml")
.defaultSuccessUrl("/index.xhtml")
.failureUrl("/login.xhtml?error=1")
.and()
.csrf().disable()
.logout().logoutSuccessUrl("/login.xhtml?logout");
所有其他 ajax 调用都在工作。 如果有人可以帮助我,那就太好了。
已编辑:当我禁用弹簧安全性时出现同样的错误。
克隆官方连接面启动器时,该错误很容易重现 https://github.com/joinfaces/joinfaces-gradle-jar-example
并添加一个 primefaces 按钮
【问题讨论】:
确定这是整个堆栈跟踪吗? 我已经更新了堆栈跟踪 能否调试并设置断点,看看内容类型究竟是什么? desiredContent 是 "text/html" 但我认为应该是 text/xml 因为部分 ajax 请求 克隆官方joinfaces starter github.com/joinfaces/joinfaces-gradle-jar-example并添加primefaces按钮时,可以轻松重现该错误 【参考方案1】:我有同样的设置和同样的问题。这是由我的主模板中的f:view
标签引起的。
<f:view contentType="text/html">
删除它可以解决问题。
【讨论】:
此修复也适用于使用 jsf .2.2 和 primefaces 6 的<p:fileUpload>
组件。谢谢
我在使用最近发布的 WildFly 14 运行稳定的应用程序时遇到了同样的问题,我还能够修复它,删除 f:view 的 contentType(f:view 标签仍然存在)。
天啊。有相同的 - 但需要 f:view 进行语言环境设置。我删除了“contentType”,然后它就起作用了。谢谢【参考方案2】:
我在将现有的 primefaces 6.2 webapp 迁移到 wildfly 14 时遇到了类似的问题。我的解决方法是将 process="@this" 添加到所有 p:menuitem
【讨论】:
【参考方案3】:正是汤姆·邦巴迪尔所说的! 如果您在 prossess 属性中有 @this,它将起作用! 就我而言,由于我使用 DefaultMenuModel 从数据库加载菜单,所以我有这个:
DefaultMenuItem mi = new DefaultMenuItem();
mi.setValue(m.getDescription());
mi.setProcess("@this"); //IMPORTANT SINCE WILDFLY 14 - Unrecognized Content Type?!
mi.setIcon("id-icon-param");
if (m.isExecutecommand())
mi.setCommand(m.getCommandinstruction());
else if (m.isLoadasdialog())
mi.setUrl(m.getViewId());
mi.setUpdate("@form");
submenu.addElement(mi);
希望对你有帮助!
植物园
【讨论】:
【参考方案4】:去掉 contentType 属性就够了:
<f:view>
【讨论】:
以上是关于Spring Boot + Primefaces - “无法识别的内容类型”异常的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 spring security 和 primefaces 处理重定向?
Spring Security + JSF 2.0 + Primefaces + Hibernate 配置
数据未显示在由 viewscope spring bean 管理的 Primefaces 选项卡中