Solved

Using object mapper in spring web application

Posted on 2016-08-11
8
88 Views
Last Modified: 2016-09-02
HI,
In my main controller code in many function i am using ObjectMapper (import org.codehaus.jackson.map.ObjectMapper;
) like :
ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(urlJsonMap);

Open in new window


        ObjectMapper mapper = new ObjectMapper();
            Bun bun = mapper.readValue(event, Bun.class);

Open in new window


I am thinking of moving it out on top of controller like :
ObjectMapper mapper = new ObjectMapper();

This way i can avoid unnecessary calls to creation of this object.
Please suggest if the above appraoch will be fine... Or alternatively is there any spring way annotation etc... i can achieve the same ?

Thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 16

Assisted Solution

by:gurpsbassi
gurpsbassi earned 150 total points
ID: 41753609
Why can't you auto wire in the object mapper?
0
 
LVL 36

Accepted Solution

by:
mccarl earned 350 total points
ID: 41753627
The approach will be fine, that object is thread safe so even if multiple threads are executing your controller code, it will be ok. I even make it final static constant of the class most of the time in my code.
1
 

Author Comment

by:Rohit Bajaj
ID: 41754835
HI,
Just to elaborate the above points and some questions also :
1) Autowiring Object Mapper as i understand is will require some entry into the xml bean config file or either i do it using @Bean Annotation inside a java class...
I was thinking of avoiding this because looks like for a simple object instatiaion which is probably needed in one class i could avoid that.
If this object was required in many classes then probably putting it into container and autowiring it would be better.

2) Can you please let me know how did you find out that ObjectMapper is thread safe in the above scenarios.

3) Regarding making it private final static

(i) I understand the final part because i will never change the object once created. But whats the need for static in this case ?
    Only reason i see is if some other class wanted to access ObjectMapper then it could do so.

Thanks
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 350 total points
ID: 41755861
In response...

1) Yes, I agree

2) It was on the Jackson wiki, but that seems to be down at the moment.

3) the "static" part just means that one instance of the ObjectMapper will be shared regardless of how many objects your controller class get instantiated. It is unlikely that Spring would created more than one instance of the controller anyway, but using static just ensures that only one ObjectMapper would be created.
0
 

Author Comment

by:Rohit Bajaj
ID: 41755866
thanks..
is there any way one can discover himself that something like object mapper is thread safe....may be i can post that as another question if that requires discussion...
0
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 350 total points
ID: 41755882
There's no concrete way to know, it's up to the developer to communicate it in some way, generally through the Javadoc or otherwise via some other documentation, such as user guide, wiki, etc.
1
 

Author Comment

by:Rohit Bajaj
ID: 41756003
Hi,
One more thing i found that there is an object mapper class :
com.fasterxml.jackson.databind.ObjectMapper
Whereas the class which i am using is org.codehaus.jackson.map.ObjectMapper

Which class were you referring to.. Or probably both of them are Thread Safe.
I found that fasterxml is the newer one. probably i should replace the older one with this new one.
0
 
LVL 16

Assisted Solution

by:gurpsbassi
gurpsbassi earned 150 total points
ID: 41756032
use whichever one your spring application comes with. You shouldn't have both on your path.
0

Featured Post

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question