I've been hitting a block while using the spring-web library (installed via Gradle, so dependencies are installed).
I know that i can use @RequestBody
to input POSTed request contents and convert it into a POJO via the Jackson Unmarshaller like so:
@RequestMapping(path = "/action", method = RequestMethod.PUT)
public MyResponsePOJO post(@RequestBody MyRequestPOJO request){
// ...
However, I want to take the POSTed contents (JSON) as a Java JSONObject
.
I've tried using @RequestBody
with JSONObject
but I get HTTP 400 errors.
There are a few methods that come to my mind to work around this:
Input the
@RequestBody
as aString
and parse it to aJSONObject
using Jackson. (This involves me manually converting the request content, which I feel Jackson or Spring should do automatically)Add a
BufferedReader
parameter to my function and parse the request content with Jackson. (Again, this involves manual conversion)Input the
@RequestBody
as aMap<String, String>
(However, my request isn't entirely String-valued)Input the
@RequestBody
as aMap<String, Object>
(This causes issues like Unchecked Casts)Second Last Resort: Create a
Request
POJO with a property asMap<String, Object>
(This causes the same issues as 4. above)Last Resort: Create POJOs for every expected input (Quite infeasible, as there are a lot of expected inputs)
So, is there any automatic way by which I can get the @RequestBody
as a JSONObject
or should I just create POJOs?
Thanks all :)