Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Requestbody to JSONObject in Spring

Tags:

java

spring

I have a class as below.

class ExampleBean{
   public String Name;
   public JSONObject data;
}

And i have @GET handler which is as follows:

@GET
@Consumes({MediaType.APPLICATION_JSON})
public Response getData(ExampleBean dataBean)
{
    // some usage code here
}

I want following json to be mapped to the ExmampleBean:

{
  "Name":"Example",
  "data":{
       "hello":"world",
       "some":"value"
   }
 }

Everything works perfectly if data was a type which had two public fields named hello and some. But since data is a JSONObject which doesn't actually have those fields or relevant setters it ends up throwing Unrecognized field "hello" (Class JSONObject), not marked as ignorable at [Source: org.apache.catalina.connector.CoyoteInputStream@17b9a4bf; line: 31, column: 18]

like image 457
Shivaprasad Bhat Avatar asked Dec 13 '25 06:12

Shivaprasad Bhat


1 Answers

Try with JsonNode. It is working.

Class: com.fasterxml.jackson.databind.JsonNode

like image 77
Ankit Adlakha Avatar answered Dec 15 '25 20:12

Ankit Adlakha