Solved

Using object mapper in spring web application

Posted on 2016-08-11
8
48 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 15

Assisted Solution

by:gurpsbassi
gurpsbassi earned 150 total points
Comment Utility
Why can't you auto wire in the object mapper?
0
 
LVL 35

Accepted Solution

by:
mccarl earned 350 total points
Comment Utility
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
Comment Utility
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
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 350 total points
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:Rohit Bajaj
Comment Utility
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
Comment Utility
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
Comment Utility
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 15

Assisted Solution

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now