【问题标题】:quarkus(vertx) how to get request body in a blocking wayquarkus(vertx)如何以阻塞方式获取请求主体
【发布时间】: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 想将正文读取为字符串,并做一些进一步的处理
  • 正文只被读取一次。如果您需要原始表单,我建议将其用作方法参数,然后在方法主体中手动进行映射

标签: jax-rs quarkus vert.x


【解决方案1】:

根据@geoand proposiiton,以下解决方案对我有用:

@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
Response create(String body) throws Exception {
    ...
}

将请求作为 String 使用是诀窍 → @Consumes(MediaType.TEXT_PLAIN)

【讨论】:

    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 2011-07-07
    • 2021-09-22
    相关资源
    最近更新 更多