java - How to add objects to JSON -
in post service, using below method parse , update database:
objectmapper mapper = new objectmapper(); jsonnode node = mapper.readtree(<string>); userlogin userlogin = mapper.convertvalue(node.get("userlogin"), userlogin.class); usersecuritydetails usersecuritydetails = mapper.convertvalue(node.get("usersecuritydetails"), usersecurity
now, in service, want send same data retieving db , adding json. suggest best way?
sample json formed:
{ "userlogin": { "user_login_id": "10011", "user_password": "password" }, "usersecuritydetails": { "user_sequence_id": "1", "seq_question_id": "1", "seq_answer": "test answer" }
}
create wrapper pojo having userlogin
, usersecuritydetails
. jackson automatically deserialize
object.
it practice expect required object instead of creating objects string.
your wrapper class like
public class securitydetailswrapper { private userlogin; private usersecuritydetails; // costructor // getters , setters }
in controller
's method can expect securitydetailswrapper
.
like
public void somefunction(@requestbody securitydetailswrapper wrapper) { // business logic }
jackson takes care of deserialization
.
Comments
Post a Comment