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
Solved

Using object mapper in spring web application

Posted on 2016-08-11
8
65 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
  • 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 35

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 35

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 35

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

791 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