I've a xml file that I'm deserializing using Jackson XmlMapper.
I wanted to do Bean validation during deserialization using javax.validation annotations like #Pattern, #Size, etc.
Is there a way to configure Jackson XmlMapper so that I can validates my POJO during deserialization?
Related
I have a akka cluster of microservices running on nodes. I'm trying to use the Jackson serializer for application specific messages exchanged between the nodes (microservices).
My code is written in scala, not Java
I'm using scala 'case class' as akka messages exchanged between actors and each message (case class) have a val LOG:org.slf4j.Logger built in to log specific business logic processing information.
When I send messages between nodes I'm getting the exception
WARN akka.remote.artery.Deserializer - Failed to deserialize message from [akka://MyCluster#127.0.0.1:25251] with serializer id [33] and manifest [com...MyCaseClassMessage]. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of org.slf4j.Logger (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: ..."[truncated 'n' bytes]; line: 'n', column: 'n'] (through reference chain: com...MyCaseClassMessage["LOG"])
My case class essentially is something like:
case class MyCaseClassMessage()
extends CborSerializable {
override val LOG:Logger = LoggerFactory.getLogger(classOf[MyCaseClassMessage])
val businessLogic:Array[Byte] = ...
def apply():Array[Byte] = ...
}
I have no idea of how to specify to Jackson how to serialize and (or) deserialize a "val LOG:Logger" in my case class. I just know that if I remove my Logger, substituing it to (for example) println("message") I don't have any problem with serialization and deserialization.
Any help?
Because Jackson relies on reflection and does not understand the convention in Scala case classes that only the constructor parameters are required for defining the message, it will attempt to serialize every field of the object.
The LOG field can be ignored by Jackson by annotating it with an #JsonIgnore annotation (com.fasterxml.jackson.annotation.JsonIgnore):
#JsonIgnore
override val LOG: Logger = LoggerFactory.getLogger(classOf[MyCaseClassMessage])
I am using Axis to generate client side service classes, and I would like to use those classes within the same runtime as the generation. I have implemented the axis generate and compile successfully. However, using reflection to instantiate and call the generated classes (with custom class loader), the jaxb unmarshall keeps returning a null (I have verified correct response xml from the service call).
It seems there needs to be some sort of initialization of the jaxb context when loading in classes in realtime (not on the classpath) from a custom classloader.
Any suggestions/insight?
Based on the below guide
https://spring.io/guides/gs/rest-service/
MappingJackson2HttpMessageConverter converts the Greeting Object to JSON. Question is, why isn't Greeting Object implementing Serializable? Is it a miss in the guide or not required?
Implementing Serializable is necessary for native Java serialization.
JSON marshalling with Jackson doesn't need it.
I have a scenario where in I have serialized and saved JSON data into a document store. The serialization was done using GSON. Now I have added an additional attribute to the class that has been serialized, so now when I try to deserialize I get an exception:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 420
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
com.google.gson.Gson.fromJson(Gson.java:795)
com.google.gson.Gson.fromJson(Gson.java:761)
com.google.gson.Gson.fromJson(Gson.java:710)
com.google.gson.Gson.fromJson(Gson.java:682)
Looks like this seems to be due to the addition of the new class attribute. Is there anyway of specifying that the new attribute is OPTIONAL so that GSON deserialization would work in this case?
GWT + Springs + DB4o: Any idea how to make this work without adding a new RPC class
[WARN] Exception while dispatching incoming RPC call
com.google.gwt.user.client.rpc.SerializationException: Type 'com.db4o.internal.query.ObjectSetFacade' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = [com.ppp.prm.portal.shared.dto.MComments#2214ab5, com.ppp.prm.portal.shared.dto.MComments#4fda105f, com.ppp.prm.portal.shared.dto.MComments#7568f5ed, com.ppp.prm.portal.shared.dto.MComments#3de9d6d3, com.ppp.prm.portal.shared.dto.MComments#4316d666, com.ppp.prm.portal.shared.dto.MComments#1055e4f3, com.ppp.prm.portal.shared.dto.MComments#772a15e5, com.ppp.prm.portal.shared.dto.MComments#6c03aa81]
at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:619)
at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamWriter.writeObject(AbstractSerializationStreamWriter.java:126)
at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter$ValueWriter$8.write(ServerSerializationStreamWriter.java:153)
at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serializeValue(ServerSerializationStreamWriter.java:539)
at com.google.gwt.user.server.rpc.RPC.encodeResponse(RPC.java:616)
Issue
RPC 101 - Objects need to implement IsSerializable or Serializable interface. None of the classes ObjectSetFacade or its super class implement them. Reference more rules - https://developers.google.com/web-toolkit/doc/latest/tutorial/clientserver
DB4o is non gwt java library. You cannot send com.db4o.internal.query.ObjectSetFacade in any RPC call.
Solution
1) You should convert from db40 objects to domain objects/pojos compatible with GWT and RPC.
2) Example - we convert Hibernate objects to Domain pojos using Dozer on server. https://developers.google.com/web-toolkit/articles/using_gwt_with_hibernate