Map JSON to existing pojo object
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have recently joined the web services world and have started to work on create and update of hibernate entities using json inputs.
The following api converts a json input to new pojo object:
Pojo newObject=mapper.readValue(jsonInput,Pojo.class);
This work well with create apis.
Now what about update apis:
I have a big pojo and i don't want to get into long method setting each value into pojo object from json input
i want something like:
Pojo existingPojo=getFromDatabase();
existingPojo=mapper.readValue(updateJsonValues,existingPojo);
saveToDatabase(existingPojo);
So whatever attributes updateJsonValues has ,they get updated into existingPojo.
This would be great help.Thanks in advance.
java json jackson
add a comment |
I have recently joined the web services world and have started to work on create and update of hibernate entities using json inputs.
The following api converts a json input to new pojo object:
Pojo newObject=mapper.readValue(jsonInput,Pojo.class);
This work well with create apis.
Now what about update apis:
I have a big pojo and i don't want to get into long method setting each value into pojo object from json input
i want something like:
Pojo existingPojo=getFromDatabase();
existingPojo=mapper.readValue(updateJsonValues,existingPojo);
saveToDatabase(existingPojo);
So whatever attributes updateJsonValues has ,they get updated into existingPojo.
This would be great help.Thanks in advance.
java json jackson
2
You can use BeanUtils or mapstruct library.
– Raheela Aslam
Nov 15 '18 at 12:35
1
You can just overwrite your existing POJO with a new POJO and the same id, same outcome
– T A
Nov 15 '18 at 12:36
Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?
– Ankur Chrungoo
Nov 15 '18 at 12:37
add a comment |
I have recently joined the web services world and have started to work on create and update of hibernate entities using json inputs.
The following api converts a json input to new pojo object:
Pojo newObject=mapper.readValue(jsonInput,Pojo.class);
This work well with create apis.
Now what about update apis:
I have a big pojo and i don't want to get into long method setting each value into pojo object from json input
i want something like:
Pojo existingPojo=getFromDatabase();
existingPojo=mapper.readValue(updateJsonValues,existingPojo);
saveToDatabase(existingPojo);
So whatever attributes updateJsonValues has ,they get updated into existingPojo.
This would be great help.Thanks in advance.
java json jackson
I have recently joined the web services world and have started to work on create and update of hibernate entities using json inputs.
The following api converts a json input to new pojo object:
Pojo newObject=mapper.readValue(jsonInput,Pojo.class);
This work well with create apis.
Now what about update apis:
I have a big pojo and i don't want to get into long method setting each value into pojo object from json input
i want something like:
Pojo existingPojo=getFromDatabase();
existingPojo=mapper.readValue(updateJsonValues,existingPojo);
saveToDatabase(existingPojo);
So whatever attributes updateJsonValues has ,they get updated into existingPojo.
This would be great help.Thanks in advance.
java json jackson
java json jackson
asked Nov 15 '18 at 12:33
nsharmansharma
688
688
2
You can use BeanUtils or mapstruct library.
– Raheela Aslam
Nov 15 '18 at 12:35
1
You can just overwrite your existing POJO with a new POJO and the same id, same outcome
– T A
Nov 15 '18 at 12:36
Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?
– Ankur Chrungoo
Nov 15 '18 at 12:37
add a comment |
2
You can use BeanUtils or mapstruct library.
– Raheela Aslam
Nov 15 '18 at 12:35
1
You can just overwrite your existing POJO with a new POJO and the same id, same outcome
– T A
Nov 15 '18 at 12:36
Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?
– Ankur Chrungoo
Nov 15 '18 at 12:37
2
2
You can use BeanUtils or mapstruct library.
– Raheela Aslam
Nov 15 '18 at 12:35
You can use BeanUtils or mapstruct library.
– Raheela Aslam
Nov 15 '18 at 12:35
1
1
You can just overwrite your existing POJO with a new POJO and the same id, same outcome
– T A
Nov 15 '18 at 12:36
You can just overwrite your existing POJO with a new POJO and the same id, same outcome
– T A
Nov 15 '18 at 12:36
Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?
– Ankur Chrungoo
Nov 15 '18 at 12:37
Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?
– Ankur Chrungoo
Nov 15 '18 at 12:37
add a comment |
2 Answers
2
active
oldest
votes
The story is that this is what ObjectMapper
-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.
The only obstacle is that there is no readValue()
-like shortcut for it (it could be something like updateValue()
), so it is a few character longer, you need to use readerForUpdating()
to get a suitable reader, and then its readValue()
:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test
public String message="Nope";
public String target="Nope";
public String toString()
return message+" "+target+"!";
public static void main(String args) throws Exception
Test test=new Test();
System.out.println(test);
ObjectMapper mapper=new ObjectMapper();
mapper.readerForUpdating(test).readValue(""message":"Hello"");
System.out.println(test);
mapper.readerForUpdating(test).readValue(""target":"World"");
System.out.println(test);
Output:
Nope Nope!
Hello Nope!
Hello World!
Edit: if it is needed repeatedly, the reader can be stored and re-used of course:
ObjectReader reader=mapper.readerForUpdating(test);
reader.readValue(""message":"Hello"");
System.out.println(test);
reader.readValue(""target":"World"");
System.out.println(test);
This works good for me. Thanks for the quick help!
– nsharma
Nov 18 '18 at 7:24
add a comment |
I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319618%2fmap-json-to-existing-pojo-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The story is that this is what ObjectMapper
-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.
The only obstacle is that there is no readValue()
-like shortcut for it (it could be something like updateValue()
), so it is a few character longer, you need to use readerForUpdating()
to get a suitable reader, and then its readValue()
:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test
public String message="Nope";
public String target="Nope";
public String toString()
return message+" "+target+"!";
public static void main(String args) throws Exception
Test test=new Test();
System.out.println(test);
ObjectMapper mapper=new ObjectMapper();
mapper.readerForUpdating(test).readValue(""message":"Hello"");
System.out.println(test);
mapper.readerForUpdating(test).readValue(""target":"World"");
System.out.println(test);
Output:
Nope Nope!
Hello Nope!
Hello World!
Edit: if it is needed repeatedly, the reader can be stored and re-used of course:
ObjectReader reader=mapper.readerForUpdating(test);
reader.readValue(""message":"Hello"");
System.out.println(test);
reader.readValue(""target":"World"");
System.out.println(test);
This works good for me. Thanks for the quick help!
– nsharma
Nov 18 '18 at 7:24
add a comment |
The story is that this is what ObjectMapper
-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.
The only obstacle is that there is no readValue()
-like shortcut for it (it could be something like updateValue()
), so it is a few character longer, you need to use readerForUpdating()
to get a suitable reader, and then its readValue()
:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test
public String message="Nope";
public String target="Nope";
public String toString()
return message+" "+target+"!";
public static void main(String args) throws Exception
Test test=new Test();
System.out.println(test);
ObjectMapper mapper=new ObjectMapper();
mapper.readerForUpdating(test).readValue(""message":"Hello"");
System.out.println(test);
mapper.readerForUpdating(test).readValue(""target":"World"");
System.out.println(test);
Output:
Nope Nope!
Hello Nope!
Hello World!
Edit: if it is needed repeatedly, the reader can be stored and re-used of course:
ObjectReader reader=mapper.readerForUpdating(test);
reader.readValue(""message":"Hello"");
System.out.println(test);
reader.readValue(""target":"World"");
System.out.println(test);
This works good for me. Thanks for the quick help!
– nsharma
Nov 18 '18 at 7:24
add a comment |
The story is that this is what ObjectMapper
-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.
The only obstacle is that there is no readValue()
-like shortcut for it (it could be something like updateValue()
), so it is a few character longer, you need to use readerForUpdating()
to get a suitable reader, and then its readValue()
:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test
public String message="Nope";
public String target="Nope";
public String toString()
return message+" "+target+"!";
public static void main(String args) throws Exception
Test test=new Test();
System.out.println(test);
ObjectMapper mapper=new ObjectMapper();
mapper.readerForUpdating(test).readValue(""message":"Hello"");
System.out.println(test);
mapper.readerForUpdating(test).readValue(""target":"World"");
System.out.println(test);
Output:
Nope Nope!
Hello Nope!
Hello World!
Edit: if it is needed repeatedly, the reader can be stored and re-used of course:
ObjectReader reader=mapper.readerForUpdating(test);
reader.readValue(""message":"Hello"");
System.out.println(test);
reader.readValue(""target":"World"");
System.out.println(test);
The story is that this is what ObjectMapper
-like things inherently do all the time, there is no other way: an object is instantiated first, and then it is updated from the JSON.
The only obstacle is that there is no readValue()
-like shortcut for it (it could be something like updateValue()
), so it is a few character longer, you need to use readerForUpdating()
to get a suitable reader, and then its readValue()
:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test
public String message="Nope";
public String target="Nope";
public String toString()
return message+" "+target+"!";
public static void main(String args) throws Exception
Test test=new Test();
System.out.println(test);
ObjectMapper mapper=new ObjectMapper();
mapper.readerForUpdating(test).readValue(""message":"Hello"");
System.out.println(test);
mapper.readerForUpdating(test).readValue(""target":"World"");
System.out.println(test);
Output:
Nope Nope!
Hello Nope!
Hello World!
Edit: if it is needed repeatedly, the reader can be stored and re-used of course:
ObjectReader reader=mapper.readerForUpdating(test);
reader.readValue(""message":"Hello"");
System.out.println(test);
reader.readValue(""target":"World"");
System.out.println(test);
edited Nov 15 '18 at 15:58
answered Nov 15 '18 at 15:52
tevemadartevemadar
4,7182827
4,7182827
This works good for me. Thanks for the quick help!
– nsharma
Nov 18 '18 at 7:24
add a comment |
This works good for me. Thanks for the quick help!
– nsharma
Nov 18 '18 at 7:24
This works good for me. Thanks for the quick help!
– nsharma
Nov 18 '18 at 7:24
This works good for me. Thanks for the quick help!
– nsharma
Nov 18 '18 at 7:24
add a comment |
I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.
add a comment |
I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.
add a comment |
I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.
I was having the same question and after a lot of digging into the dirt I came across an open source library, MapStruct. It helps in java beans mapping and generates code for you at the time your application boots up. It worked fine for me. Give it a go.
answered Nov 15 '18 at 13:46
abj1305abj1305
190112
190112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319618%2fmap-json-to-existing-pojo-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You can use BeanUtils or mapstruct library.
– Raheela Aslam
Nov 15 '18 at 12:35
1
You can just overwrite your existing POJO with a new POJO and the same id, same outcome
– T A
Nov 15 '18 at 12:36
Agree (with @TA ), overwrite is the way to go. Why do you want to read the existing pojo values from the DB and then update it?
– Ankur Chrungoo
Nov 15 '18 at 12:37