@RequestBody的替代方案 [英] Alternative of @RequestBody

查看:949
本文介绍了@RequestBody的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有其他方法可以在不使用注解的情况下获取对请求体的引用?我使用 GAE + Spring ,并且每次在我的控制器方法的签名中使用 @RequestBody 时,服务器都会返回 415不支持的媒体类型。我所要做的就是在Post方法上读取 JSON编码的消息。感谢。

Is there an alternative way to obtain a reference to a request's body without using the approach with the annotation? I'm using GAE + Spring and everytime I use @RequestBody in the signatures of my controller methods, the server returns 415 Unsupported Media Type. All I'm trying to do is read a JSON encoded message on a Post method. Thanks.

推荐答案

您可以接收 HttpServletRequest 的参数,使用 ServletInputStream 29rel =nofollow> getInputStream() 。该流将包含请求的主体。

You can take in a parameter of HttpServletRequest, and read the ServletInputStream using getInputStream() from there. The stream will contain the body of the request.

@Controller
public class MyController {
    @RequestMapping("/test")
    public String aTest(HttpServletRequest request) {
        InputStream body = request.getInputStream();
        //process request body
        return myReturnVal;
    }
}

这篇关于@RequestBody的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆