我正在尝试编写一个可以使用任何类型的内容并从请求中获取原始内容的micronaut端点,并且面临着两个挑战:
- 如何编写端点,例如无论内容类型如何,请求都会被路由到该端点? (micronaut默认消耗到application / json)
- 如何阅读此类请求的正文?
我尝试了以下方法:
@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一样实现我的处理程序。