Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using object mapper in spring web application

Posted on 2016-08-11
8
Medium Priority
?
110 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 600 total points
ID: 41753609
Why can't you auto wire in the object mapper?
0
 
LVL 36

Accepted Solution

by:
mccarl earned 1400 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 1400 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 1400 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 600 total points
ID: 41756032
use whichever one your spring application comes with. You shouldn't have both on your path.
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

722 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