Reputation: 2928
I have a controller that receives a JSON RequestBody
like the one below:
{
"status":"pending",
"status1":"pending",
"status2":"pending",
"status3":"pending",
"specs":{
"width":1000,
"height":1507,
........some other fields, any number of fields..
},
}
I have an Entity
@Entity
public class MyBean implements Serializable {
@Id
@GeneratedValue
Long id;
public String status;
public String status1;
public String status2;
public String status3;
}
and my Controller class:
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public void insert(@RequestBody MyBean myBean) {
return myService.save(myBean);
}
My problem is that I want to store the value of specs
(which is a JSON document) as a String in a Lob field and I don't know how to do that.
Upvotes: 1
Views: 422
Reputation: 306
Declare specs as public Map<String,Integer> specs;
and to convert Object to json use Jackson fasterxml api as below
MyBean bean=new MyBean();
bean.setId(new Long(1));
bean.setStatus("pending");
bean.setStatus1("pending");
bean.setStatus2("pending");
bean.setStatus3("pending");
Map<String, Integer> temp=new HashMap<String, Integer>();
temp.put("width",1000);
temp.put("height",1507);
bean.setSpecs(temp);
//Object to json
StringWriter sw=new StringWriter();
ObjectMapper mapper=new ObjectMapper();
mapper.writeValue(sw,bean);
System.out.println(sw.toString());
//json to object
MyBean newBean=mapper.readValue(sw.toString(), MyBean.class);
System.out.println(newBean.toString());
If you want serialize only specs before saving then use same mapper.writeValue() function to convert specs to json string for more information refer this
Upvotes: 2