• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing nested POJO Objects  RSS feed

 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Hope everyone is good.

I have a scenario where I have a web service response in XML mapped to a POJO object and returned.

This POJO object is having nested nodes.

I need to read a particular node value (which is in a inner level) and print it as a common value for all services.

Looks like the below.



I need to use null check at all places to access the values and the code looks ugly.



Also ,the POJO object does not have a method like fieldA.hasValue() which checks for the null condition.

I need to write a common utility for printing this data which is common for all web services.

I'm using Java 1.7.

Could someone explain is there any other Utility or library that I could use or how to achieve this ?

Thanks
Karthik S
 
Stephan van Hulst
Saloon Keeper
Posts: 7972
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use Optionals:

 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan,

Thanks for the reply. I would love using Optional but currently i'm restricted to use Java 1.6/1.7.

So looking for something within that.

 
Stephan van Hulst
Saloon Keeper
Posts: 7972
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah sorry, I missed that.

Maybe you can tell us more about how you're binding XML to POJOs. Are you using JAX-B or something similar?

you should separate your data and business layers. When using JAX-B you can use XmlAdapter to map between data transfer objects and business objects. Do the validation in your adapter, so that your business objects are always valid. Then you can call methods on your business objects without fear.
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya Stephen,

Actually we are using Apache Axis (wsdl2Java) to generate the POJO classes from the web services.

This request is actually for logging purpose .

I thought of writing a Utility method where these values(status , details , etc) if present could be written on the log and could use it in all places.

For certain responses , we will get nested values. For some we wouldn't have it.



 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Questions like this one make me regret that one particular piece of syntax was rejected. I can't remember who proposed it or where I read it, but someone suggested using ?. as the "optional operator". ?. would call the member (method, field) of the object, unless if it's null. In the presence of null any return value would be the default for the type (0, false, null, etc).

In your case, the syntax would have been Object?.getXYZ()?.getStatusDesc()?.getMsg(), and the result would have been the message if no nulls were encountered, and null otherwise.

Unfortunately though, this was shot down, and it will probably never come to be. Optional is the closest we're going to get.
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I have one more question.

My application is having multiple web services and I am trying to write a common utility for logging the response data.

The response is a POJO of a particular class (different POJOs for each service) and has certain values like Status , Details ,etc.

I need to get those values and log it..

For ex)




This stands okay for a single POJO.

But since I have too many different types of POJOs , i thought of writing a common utility which takes care of logging the data.

Reflection was an option but it looks too costly. And as well , i guess for logging , it is never a right option as huge data would keep on coming.

Suggest me !!!

Thanks..







 
Tim Holloway
Saloon Keeper
Posts: 18792
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A common logger component would be a fairly clean solution. Just pass it the request and response objects.

If you'd rather not slap an explicit method call into each of your handler POJOs, you could always consider using Aspect-Oriented Programming. AOP will automatically decorate designated methods with an implicit call to your logger. Of course, it might also drive you insane.

You could probably also do things using annotations.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!