micronaut-如何接受任何请求的内容和正文

我正在尝试编写一个可以使用任何类型的内容并从请求中获取原始内容的micronaut端点,并且面临着两个挑战:

  1. 如何编写端点,例如无论内容类型如何,请求都会被路由到该端点? (micronaut默认消耗到application / json)
  2. 如何阅读此类请求的正文?

我尝试了以下方法:

@Controller(value = "/test",consumes = "*/*") 
public class MyController {

   @Post("/one")
   public String one(HttpRequest<?> req) {
      // req.getHeaders() returns expected headers
      // req.getParameters() seems to be fine
      // req.getBody(...) always return null regardless of which getBody method I use. I used the debugger to study what `req` contains and saw the underlying netty content appears empty
   }

   @Post("/two")
   public String two(HttpHeaders headers,HttpParameters params,@Body Object value) {
      // headers & params are good
      // body gives me a CompositeByteBuf... I find it surprising micronaut "leaks" the underlying netty bytebuf to the higher level impl 
   }

}

注意事项:

  • 只有在卷曲中通过-H "Content-Type: */*"时,我才能击中这些端点。我希望无论Content-Type的值如何,这些端点都是可以到达的。
  • 第一个处理程序不显示任何正文,而第二个处理程序显示对于相同的请求有一个正文(只是指向另一条路径)。我也更喜欢像处理程序#1一样实现我的处理程序。
wangliqingkong 回答:micronaut-如何接受任何请求的内容和正文

通配符是问题所在,我能够使处理程序#1与HttpRequest<String>一起使用:

@Post("/one")
public String one(HttpRequest<String> req) { ...

,尽管consumes="*/*",处理程序仍然不接受任何内容类型的请求。我向项目提交了一个问题:https://github.com/micronaut-projects/micronaut-core/issues/2334

本文链接:https://www.f2er.com/3115045.html

大家都在问