我正在尝试使用Resteasy Docs中所述的嵌入式Netty实例使用Resteasy测试资源。
注入路径参数和查询参数的工作原理就像一个魅力,但后来我试图测试资源内喷射HttpServletRequest
,并HttpServletResponse
从这样的背景下:
@GET
@Path("/")
public void example(@Context HttpServletResponse response,
@Context HttpServletRequest request) { ... }
ResteasyHttpServletRequest
在上下文中找不到,并引发以下异常:
5105 [r #1] DEB o.j.resteasy.core.SynchronousDispatcher - PathInfo: /auth
5201 [r #1] ERR c.s.f.v.s.r.e.ApplicationExceptionMapper - Unhandled application exception: Unable to find contextual data of type: javax.servlet.http.HttpServletRequest
org.jboss.resteasy.spi.LoggableFailure: Unable to find contextual data of type: javax.servlet.http.HttpServletRequest
我尝试按照RESTEasy Mock与Exception Mapper与Context中的建议,将模拟的请求和响应版本放入上下文中,但由于上下文数据是ThreadLocal,Netty为每个请求生成了一个新线程,因此它也不起作用。
关于如何解决这个问题的任何想法?
我只是在另一个项目上再次受到打击,决定再次进行调查。
问题是在Netty的模拟请求中,没有HttpServletRequest
可用的请求。如果您查看NettyJaxrsServer
类和相关类的源代码,Reasteasy将其自己的抽象用于未实现的http请求HttpServletRequest
。
如果更改实现以使用这些抽象,则可以访问资源中的请求和响应。
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse;
@GET
@Path("/")
public void example(@Context HttpResponse response,
@Context HttpRequest request) { ... }
这不是完美的,因为它使我的资源依赖于Resteasy接口,但是我现在决定使用它来支持多部分数据的模拟测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句