Alternative of @RequestBody

2019-07-26 05:57发布

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.

2条回答
beautiful°
2楼-- · 2019-07-26 06:09

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;
    }
}
查看更多
Melony?
3楼-- · 2019-07-26 06:34

Try this, on the RequestMapping configure the headers to accept application/json and be sure to configure a jackson message convertor for this type

查看更多
登录 后发表回答