【发布时间】:2017-04-14 02:13:27
【问题描述】:
使用 Apache CXF 依赖 cxf-rt-transports-http-jetty-3.1.4.jar 来测试 JAXRS 服务。以下是 Junit 注释-
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:test-spring-context.xml"})
web.xml(不要认为web.xml是由@WebAppConfiguration加载的,而是在标准目录src/main/webapp/WEB-INF/web.xml)
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- listener to inject HttpServlet Request -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- CXF Servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
码头配置-
<httpj:engine-factory bus="cxf" id="port9191">
<httpj:engine port="9191">
<httpj:threadingParameters minThreads="5"
maxThreads="15" />
<httpj:handlers>
<bean class="org.eclipse.jetty.server.handler.DefaultHandler" />
</httpj:handlers>
<httpj:sessionSupport>true</httpj:sessionSupport>
</httpj:engine>
</httpj:engine-factory>
尝试从@Autowired HttpServletRequest request 对象request.getHeader("ApplicationName"); 读取标头时遇到异常-
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:309) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:64) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:325) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:320) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:307) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
任何帮助将不胜感激!
【问题讨论】:
-
src/main/webapp/web.xml 不是标准位置。放入src/main/webapp/WEB-INF/web.xml。也许这就是它没有注意到您已将 RequestContextListener 声明为侦听器的原因。
-
我错误地在帖子中输入了错误的路径。它仅在 src/main/webapp/WEB-INF 内。我现在更新了问题。
标签: java spring jetty jax-rs cxf