Solved

Using object mapper in spring web application

Posted on 2016-08-11
8
76 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 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 116
jsp error 6 72
Running JavaFX on the Raspberry Pi 27 292
Selenium findElement(By.classname  identifier 15 35
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 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

738 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