I'm using Apache Flink (v1.11) with Scala and added an own DeserializationSchema for Kafka connector. Therefore i would like to use my own packages and versions of jackson (v2.12.0).
But i got the following error:
Exception in thread "main" java.lang.VerifyError: Cannot inherit from final class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at com.fasterxml.jackson.dataformat.csv.CsvMapper.<init>(CsvMapper.java:108)
at de.integration_factory.datastream.types.CovidEventSchema.<init>(CovidEventSchema.scala:14)
at de.integration_factory.datastream.Aggregate_Datastream$.main(Aggregate_Datastream.scala:34)
at de.integration_factory.datastream.Aggregate_Datastream.main(Aggregate_Datastream.scala)
This is my EventSchema:
import com.fasterxml.jackson.dataformat.csv.CsvMapper
import com.fasterxml.jackson.datatype.joda.JodaModule
import org.apache.flink.api.common.serialization.{DeserializationSchema, SerializationSchema}
import org.apache.flink.api.common.typeinfo.TypeInformation
#SerialVersionUID(6154188370181669758L)
class CovidEventSchema extends DeserializationSchema[CovidEvent] with SerializationSchema[CovidEvent] {
private val mapper = new CsvMapper
mapper.registerModule(new JodaModule)
val csvSchema = mapper
.schemaFor(classOf[CovidEvent])
.withLineSeparator(",")
.withoutHeader()
val reader = mapper.readerWithSchemaFor(classOf[CovidEvent])
def serialize(event: CovidEvent): Array[Byte] = mapper.writer(csvSchema).writeValueAsBytes()
#throws[IOException]
def deserialize(message: Array[Byte]): CovidEvent = reader.readValue[CovidEvent](message)
def isEndOfStream(nextElement: CovidEvent) = false
def getProducedType: TypeInformation[CovidEvent] = TypeInformation.of(classOf[CovidEvent])
}
This is my PoJo for schema:
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.joda.time.DateTime;
#Data
#NoArgsConstructor
#AllArgsConstructor
public class CovidEvent {
private long objectId;
private int bundeslandId;
private String bundesland;
private String landkreis;
private String altersgruppe;
private String geschlecht;
private int anzahlFall;
private int anzahlTodesfall;
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTC")
private DateTime meldedatum;
private int landkreisId;
private String datenstand;
private int neuerFall;
private int neuerTodesfall;
private String refDatum;
private int neuGenesen;
private int anzahlGenesen;
#JsonFormat(shape = JsonFormat.Shape.NUMBER)
private boolean istErkrankungsbeginn;
private String altersGruppe2;
public long getEventtime() {
return meldedatum.getMillis();
}
}
After some research I found out that the error is probably caused by different Jackson versions in the classpath.
I thought it would be possible to use own version of Jackson, because Flink shaded the own versions.
What am I doing wrong?
UPDATE: If i import the jackson classes from shaded flink package it is working
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper
But so i am dependent on the flink shaded jackson version.
UPDATE: So a better implementation by using open is something like this ?
class CovidEventSchema extends DeserializationSchema[CovidEvent] with SerializationSchema[CovidEvent] {
private var reader: ObjectReader = null
private var writer: ObjectWriter = null
override def open(context: SerializationSchema.InitializationContext): Unit = {
val mapper = new CsvMapper()
val csvSchema = mapper
.schemaFor(classOf[CovidEvent])
.withLineSeparator(",")
.withoutHeader()
this.reader = mapper.readerFor(classOf[CovidEvent]).`with`(csvSchema)
this.writer = mapper.writer(csvSchema)
super.open(context)
}
}
It would work if the classloader of Flink are used. However, the way your setup works, you are just loading your user code in the system classloader while the whole DataStream application is created. I'm not going to much into more details (unless requested in a follow-up) and go for the solution:
Your DeserializationSchema should never initialize heavy resources during creation (this happens on client or job manager side), but only in open (which happens on task manager). So please move
private val mapper = new CsvMapper
mapper.registerModule(new JodaModule)
into open.
It only works with the bundled version because - lucky for you - ObjectMapper implements Serializable but that is rarely the case for parsers and actually completely unnecessary if the deserializer is initialized correctly.
Related
#Data
public class IdentificacaoBiometricaDto {
private Integer cdIdentifBiom;
private String nrMatricula;
private String deImpressaoDigital;
private Integer cdFilialAtualizacao;
}
I am using retrofit 2.6.1, jackson 2.9.9 and lombok 1.8.10.
The exception is:
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class br.com.clamed.modelo.loja.dto.central.IdentificacaoBiometricaDto and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:313)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:400)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1392)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1120)
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsBytes(ObjectWriter.java:1017)
at retrofit2.converter.jackson.JacksonRequestBodyConverter.convert(JacksonRequestBodyConverter.java:34)
at retrofit2.converter.jackson.JacksonRequestBodyConverter.convert(JacksonRequestBodyConverter.java:24)
at retrofit2.ParameterHandler$Body.apply(ParameterHandler.java:355)
... 14 more
The object mapper:
return new ObjectMapper().registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
I am setting all fields, when passing it to a request body, retrofit fails because jackson could not serialize the object.
Retrofit call:
#POST("/usuario/v1.0/cadastraBiometria")
Call<IdentificacaoBiometricaDto> cadastraBiometria(#Body IdentificacaoBiometricaDto identificacaoBiometricaDto);
Rest service:
#RestController
#RequestMapping("/usuario")
public class UsuarioController {
#PostMapping(value = "/v1.0/cadastraBiometria")
public ResponseEntity<IdentificacaoBiometricaDto> cadastraBiometria(#RequestBody IdentificacaoBiometricaDto identificacaoBiometricaDto) {
}
}
Update:
If I change the retrofit converter to Gson it works;
If I serialize it using Jackson directly, it works;
Removing lombok makes no difference;
Found the problem. The biometric reader library was causing this. For some reason it's incompatible with openjdk-11 and is causing all sort of unrelated problems.
Yes, very weird. But the lib is very poorly done.
I am serializing a guava optional using jackson(version:2.9.4).
When i am deserializing, i am getting below exception:
Cannot deserialize instance of 'java.lang.Long' out START_OBJECT token.
When i debug the value during deserialization, i noticed that the value is coming as "Optional(present=true)" rather than the value present inside Optional.
I looked at the object mapper, and "GuavaModule()" is already being registered with the object mapper.
Please let me know if i am missing anything.
With registering the GuavaModule of
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
</dependency>
…and the following bean…
import com.google.common.base.Optional;
import lombok.Data;
import lombok.NoArgsConstructor;
#Data
#NoArgsConstructor
public class GuavaBean {
private Optional<Long> abc;
}
…this gives me a green test…
import static org.assertj.core.api.Assertions.assertThat;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
[…]
#Test
public void guava() throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new GuavaModule());
GuavaBean bean = new GuavaBean(Optional.of(9l));
String beanSerialized = objectMapper.writeValueAsString(bean);
assertThat(beanSerialized).isEqualTo("{\"abc\":9}");
GuavaBean optLong = objectMapper.readValue(beanSerialized, GuavaBean.class);
assertThat(optLong.getAbc().get()).isEqualTo(9);
}
However, if I accidentally use java.util.Optional instead of com.google.common.base.Optional, then the beahviour is exactly like you wrote.
I'm getting the below error while trying to save a search results using the Remote interface for SearchFacade.java
"Failed to marshal EJB parameters"
Can not set org.apache.commons.collections.FastHashMap field
org.apache.commons.validator.Field.hMsgs to
org.apache.commons.collections.FastHashMap at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at
I'm Using struts 1.1, EJB 2.1 using xdoclet 1.2.3 jars for generating the dependency files.(which is inevitable to use), Where my Local,Home interfaces are being generated using Xdoclet..
I'm also using Java 6, Jboss EAP 6.1 Alpha in my project.
Note: The same code works fine when running in Jboss 4.0
So wonder is my remote calling is correct.
Any help is welcome.
Error Logs
java.lang.RuntimeException: JBAS014154: Failed to marshal EJB parameters at org.jboss.as.ejb3.remote.LocalEjbReceiver.clone(LocalEjbReceiver.java:270) at org.jboss.as.ejb3.remote.LocalEjbReceiver.clone(LocalEjbReceiver.java:259) at org.jboss.as.ejb3.remote.LocalEjbReceiver.processInvocation(LocalEjbReceiver.java:170) at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181) at org.jboss.ejb.client.EJBHomeCreateInterceptor.handleInvocation(EJBHomeCreateInterceptor.java:79) at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:183) at org.jboss.ejb.client.TransactionInterceptor.handleInvocation(TransactionInterceptor.java:42) at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:183) at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:125) at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:183) at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:177) at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:161) at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:124) at $Proxy25.saveSearch(Unknown Source) at com.web.history.SearchFormDelegate.saveSearch(SearchFormDelegate.java:177) at com.history.SaveSearchAction.createNewSavedSearch(SaveSearchAction.java:109) at com.history.SaveSearchAction.executeSynchronized(SaveSearchAction.java:296) at com.dispatch.SynchronizedAction.execute(SynchronizedAction.java:206) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) at com.security.AuthenticationFilter.doFilter(AuthenticationFilter.java:672) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) at com.planetj.servlet.filter.compression.CompressingFilter.doFilter(CompressingFilter.java:270) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.IllegalArgumentException: Can not set org.apache.commons.collections.FastHashMap field org.apache.commons.validator.Field.hMsgs to org.apache.commons.collections.FastHashMap at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150) at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63) at java.lang.reflect.Field.set(Field.java:657) at org.jboss.marshalling.cloner.SerializingCloner.storeFields(SerializingCloner.java:368) at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:313) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:253) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:134) at org.jboss.marshalling.cloner.SerializingCloner.cloneFields(SerializingCloner.java:348) at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:309) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:253) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:134) at org.jboss.marshalling.cloner.SerializingCloner$StepObjectInput.doReadObject(SerializingCloner.java:836) at org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:37) at org.jboss.marshalling.MarshallerObjectInputStream.readObjectOverride(MarshallerObjectInputStream.java:57) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:344) at java.util.HashMap.readObject(HashMap.java:1030) at sun.reflect.GeneratedMethodAccessor119.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.jboss.marshalling.reflect.SerializableClass.callReadObject(SerializableClass.java:218) at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:302) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:253) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:134) at org.jboss.marshalling.cloner.SerializingCloner.cloneFields(SerializingCloner.java:348) at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:309) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:253) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:134) at org.jboss.marshalling.cloner.SerializingCloner.cloneFields(SerializingCloner.java:348) at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:309) at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:285) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:253) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:134) at org.jboss.marshalling.cloner.SerializingCloner.cloneFields(SerializingCloner.java:348) at org.jboss.marshalling.cloner.SerializingCloner.initSerializableClone(SerializingCloner.java:309) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:253) at org.jboss.marshalling.cloner.SerializingCloner.clone(SerializingCloner.java:134) at org.jboss.as.ejb3.remote.LocalEjbReceiver.clone(LocalEjbReceiver.java:268) ... 42 more
Code:
saveAction.java
protected void newSavedSearch(final SrchFrmDelegate sfd,
final String userId, final HttpServletRequest request,
final SaveSearchForm form) throws RemoteException,
UsrNotFoundException {
BseSrchValue srchValue = SrchResultsAction.retrieveSrchCriteria(request);
FrmLayout frmLayout = (FrmLayout) request.getSession().getAttribute(
FrmBuilderAction.FRM_LAYOUT_KEY);
Integer resultCount = null;
SrchResultValue srchResult = SearchResultsAction.retrieveSearchResults(request);
if (srchResult != null) {
resultCount = new Integer(srchResult.getTotal());
}
sfd.saveSearch(userGuid,
form.getTitle(),
form.getDesc(),
form.getNewTtle(),
srchValue,
frmLayout,
resultCount,
form.getSearches());
}
SrchFrmDelegate.java
/**
* Reference to the remote interface.
*/
private SrhFrmFacadeRemote srhFacadeRemote;
public String saveSearch(final String userId, final String srchTtle,
final String srchDesc, final Boolean newTtle,
final BsSearchValue srchValue, final FrmLay frmLay,
final Integer resultCount, final List alerts)
throws UsrNotFoundException,
RemoteException {
return srhFacadeRemote.saveSearch(userId, srchTtle,
srchDesc, newTtle, srchValue, frmLay,
resultCount, alerts);
}
SrchFrmFacadeRemote.java
/**
* Remote interface for SrchFrmFacade.
*/
public java.lang.String saveSearch( java.lang.String userId,java.lang.String srchTtle,java.lang.String srchDesc,java.lang.Boolean newTtle,com.common.search.BsSearchValue srchValue,com.common.search.advanced.FrmLay frmLay,java.lang.Integer resultCount,java.util.List alerts ) throws com.common.admin.UserNotFoundException, java.rmi.RemoteException;
Objects that passed as parameters or as response on remote calls must be Serializable.
example:-
public class APIGetVerificationProofs implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String fileName;
private String mimeType;
private String itemId;
private String type;
.
.
.
.
}
After adding the below jars in Jboss 7 modules & altering the standalone.xml file accordingly, Issue is fixed.
jboss-eap-6.1.0.Alpha\modules\system\layers\base\org\apache\commons\validator
commons-validator-1.1.0.jar
jboss-eap-6.1.0.Alpha\modules\system\layers\base\org\apache\commons\collections commons-collections-3.2.1.jar
In Standalone.xml
<subsystem xmlns="urn:jboss:domain:ee:1.1">
<spec-descriptor-property-replacement>false</spec-descriptor-property-replacement>
<jboss-descriptor-property-replacement>true</jboss-descriptor-property-replacement>
<global-modules>
<module name="org.apache.commons.collections" slot="main"/>
<module name="org.apache.commons.validator" slot="main"/>
</global-modules>
</subsystem>
In EJB3 this error can also occur when invoking an EJB Session Bean across a #Remote view of the EJB. Depending on the design and usage scenario, you may be able to invoke the EJB over a #Local interface. Using the #Local interface will eliminate the requirement for the parameters to be Serializable but this can markedly change your design. If using the #Local view is an option for you, you can simply add the #Local to the EJB without removing the #Remote.
public class MySessionBeanImpl implements MySessionBean {
...
}
#Local
#Remote
public interface MySessionBean {
...
}
The JEE App Server will make the right decision to use the local view if the client of the invocation is in-container. According to the API docs, the Local annotation can appear on either the class or the interface. If on the class, the value attribute must be specified.
remember to add serialVersionUID to let class to be serialized and restored (generate it as getter or setter)
For this problem first, you have to implement Serializable interface in your POJO class (java beans) as:
package com.test;
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private int empID;
private String empName;
private int empSalary;
public int getEmpID() {
return empID;
}
//alt_shift_s_r-getter and setter method
//alt_shift_s_s override toString()
public void setEmpID(int empID) {
this.empID = empID;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getEmpSalary() {
return empSalary;
}
public void setEmpSalary(int empSalary) {
this.empSalary = empSalary;
}
#Override
public String toString() {
return "Employee [empID=" + empID + ", empName=" + empName + ", empSalary=" + empSalary + "]";
}
}
Note: if you are adding some external jars into your project then follow the below procedure to add jars.
Right Click on your EJB project and go to properties.
Then Select Deployment assembly then clicks on add.
From the appears option select appropriate source (As Archive from File System) then go to next.
Now Add jars from your desired location and finish the procedure.
enter code here
I have the following simple class:
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
#JsonIgnoreProperties({ "thirdField" })
public class Message {
private TypeA type;
private String producer;
//Getters and Setters
}
in my test class
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public void testMethd() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.USE_ANNOTATIONS, true);
Class<T> instanceType = Message.class;
String msgBody = "{\"producer\": \"clientApp\", \"type\": \"aType\", \"thirdField\": []}";
objectMapper.readValue(msgBody, instanceType);
}
}
All I am trying to do is to convert the above json string into Message class and ignore the 'thirdField'. But I keep getting
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "thirdField" (class Message), not marked as ignorable (2 known properties: , "type", "producer"])
You've mixed different versions of Jackson.
Notice that you import JsonIgnoreProperties from org.codehaus.jackson.annotate (version 1.x)
while you're using ObjectMapper from com.fasterxml.jackson.databind (version 2.x).
Try using the last Jackson version (2.4):
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
#JsonIgnoreProperties({"id"})
Here you can find an example where it's implement using version 2.4:
http://www.ibm.com/developerworks/java/library/j-hangman-app/index.html
I found a Solution to this.
Try to add
#JsonSerialize(include= JsonSerialize.Inclusion.NON_EMPTY)
About your class
#JsonSerialize(include= JsonSerialize.Inclusion.NON_EMPTY)
class ResponseModel {
//your properties here
#JsonIgnoreProperties("messageList","contactList","sender")
var contactList= ArrayList<ContactModel>()
}
That will solve your issue buddy.
It didn't work for me any of the above answers, i found a workaround that I have reinitialized the object and values (copied the object).
I am using Spring Integration to consume a message with a JSON Payload.
In my spring context I have
<integration:channel id="jsonToMyMessageConverterChannel"/>
<integration:json-to-object-transformer
type="com.acme.messaging.message.MyMessage"
input-channel="jsonToMyMessageConverterChannel"
output-channel="myMessageUpdateChannel"/>
My message related objects are:
MyMessage.java
#JsonIgnoreProperties(ignoreUnknown=true)
public class MyMessage {
#JsonProperty
private String timestamp;
#JsonProperty("msgs")
private List<Message> messages;
// Getters and Setters...
}
Message.java
#JsonIgnoreProperties(ignoreUnknown=true)
public class Message {
#JsonProperty
private Integer msgId;
#JsonProperty("msgText")
private String text;
// Getters and Setters...
}
When the json transformer attempts to convert the message to an object it fails with
Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "msgs" (Class com.acme.messaging.message.MyMessage), not marked as ignorable
The JSON payload definitely has msgs which is an array that has objects which represent the Message.java class.
Can any one suggest reasons why the exception occurs given that the JSON has the field that is being complained about and the class itself is also annotated to ignore unknown fields?
Update
After some debugging it looks like the #JsonProperty("msgs") annotations aren't being use, for some reason.
This works fine for me...
#Test
public void test() throws Exception {
MyMessages mm = new MyMessages();
MyMessage m = new MyMessage();
m.setMsgId(1);
m.setText("foo");
mm.setMessages(Arrays.asList(m));
mm.setTimestamp("123");
#SuppressWarnings("deprecation")
ObjectToJsonTransformer otjt = new ObjectToJsonTransformer(new ObjectMapper());
Message<?> message = new GenericMessage<MyMessages>(mm);
message = otjt.transform(message);
System.out.println(message);
#SuppressWarnings("deprecation")
JsonToObjectTransformer<MyMessages> jtot = new JsonToObjectTransformer<MyMessages>(MyMessages.class, new ObjectMapper());
message = jtot.transform(message);
mm = (MyMessages) message.getPayload();
System.out.println(mm.getTimestamp());
System.out.println(mm.getMessages().get(0).getText());
}
(I changed your classnames slightly to avoid colliding with Message<?>)
Resulting in...
[Payload={"timestamp":"123","msgs":[{"msgId":1,"msgText":"foo"}]}][Headers={timestamp=1373997151738, id=f2425f36-a500-4aee-93a4-e7e0240ce0f1, content-type=application/json}]
123
foo
Do you have both jackson 1.x (codehaus) and 2.x (fasterxml) on the classpath, and using Spring Integration 3.0.0?
If they're both on the classpath, SI will use Jackson 2.x, by default, (which won't understand 1.x annotations).
Or, I guess - are you using Jackson2 (fasterxml) annotations? Spring Integration 2.x uses Jackson 1 (codehaus).
EDIT:
In order to support both versions of Jackson, you can annotate the class with both annotations...
#JsonProperty("msgs")
#com.fasterxml.jackson.annotation.JsonProperty("msgs")
public List<MyMessage> messages;