Saturday, July 25, 2015
Thursday, July 16, 2015
Serialize manually using faster jackson library in spring
I am trying to get a JSON Structure for my object Order which internally has OrderDetails as oneToMany mapping,
objMapper.writerWithType(objMapper.getTypeFactory().constructCollectionType(List.class,Order.class)).writeValueAsString(orderObjs);
@Entity
public class Order {
/*
All the fields associated with this class
*/
@OneToMany(fetch = FetchType.EAGER, mappedBy = "orderId")
private Set<OrderDetails> orderDetails;
//Getters for all properties defined in this class as jackson would depend on this thing
}
In my case I am using a textWebSocket which is expecting a message only in String format so I need to serialize the objects and push to the client, I am depending on faster jackson to do it and here it goes,public String getObjectAsString()
{
//orderObjs : Considering this is the list of objects of class Order
ObjectMapper objMapper = new ObjectMapper();
returnValue = objMapper.writerWithType(
objMapper.getTypeFactory().constructCollectionType(
List.class, Order.class)).writeValueAsString(
orderObjs);
return returnValue;
}
objMapper.writerWithType(objMapper.getTypeFactory().constructCollectionType(List.class,Order.class)).writeValueAsString(orderObjs);
Monday, July 6, 2015
Use a new keyword and still inject the object : Spring
Spring works on dependency injection. This means there is no need to create an object explicitly, spring will do it for you.
But there are situations where you have to create new objects (ex., Reflection). By going with this approach you are conveying string that you will have control over the current object. i.e., any Autowired fields inside the object class will be null and spring will not consider injecting it.
But there is a way to autowire those fields as well,
appctx.getAutowireCapableBeanFactory().autowireBean(wsdlParserAPIObject);
where appctx is ApplicationContext which is autowired.
http://stackoverflow.com/questions/31237062/using-class-forname-but-want-to-autowire-members-of-the-target-class
But there are situations where you have to create new objects (ex., Reflection). By going with this approach you are conveying string that you will have control over the current object. i.e., any Autowired fields inside the object class will be null and spring will not consider injecting it.
But there is a way to autowire those fields as well,
appctx.getAutowireCapableBeanFactory().autowireBean(wsdlParserAPIObject);
where appctx is ApplicationContext which is autowired.
http://stackoverflow.com/questions/31237062/using-class-forname-but-want-to-autowire-members-of-the-target-class
Subscribe to:
Posts (Atom)