【发布时间】:2022-01-26 02:30:25
【问题描述】:
- 我的尝试:
注入CurrentVertxRequest 上下文,然后从那里获取正文
@Path("/map_event")
public class MapEventApi {
@Inject
CurrentVertxRequest reqContext;
@POST
@Consumes({ "application/json" })
@Produces({ "application/json" })
Response create(@Valid MapEvent mapEvent, @Context SecurityContext securityContext) throws Exception {
String body = reqContext.getCurrent().getBodyAsString();
...
}
}
但这会给出警告:
2022-01-25 18:22:08,854 WARN [null:-1] (executor-thread-0) BodyHandler in not enabled on this route: RoutingContext.getBodyAsString(...) in always be NULL
- 再试一次:
注入标准 JaxRS HttpServletRequest 上下文
@Context HttpServletRequest
会得到这个错误:
org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.servlet.http.HttpServletRequest
at org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:155)
at com.sun.proxy.$Proxy97.getInputStream(Unknown Source)
我猜这是因为 quarkus 在后台使用了 vertx,因此注入常规 jaxrs 上下文将不起作用,因为它不是同一个线程。
【问题讨论】:
-
你的代码是什么样的?
-
@geo 我已将代码更新为原始帖子
-
你想把正文读成什么?
-
@geoand 想将正文读取为字符串,并做一些进一步的处理
-
正文只被读取一次。如果您需要原始表单,我建议将其用作方法参数,然后在方法主体中手动进行映射