Reputation: 1175
I am facing issue while converting message into Object format after consuming message at consumer end. I couldn't able to convert back to Student object. FYI, at producer end am using spring RabbitTemplate and at consumer end plain java api(Note#: I cannot use spring at consumer end)
Issue: org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class com.steelwedge.util.Student] from JSON String; no single-String constructor/factory method at org.codehaus.jackson.map.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:379) at org.codehaus.jackson.map.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:268) at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromString(BeanDeserializer.java:765) at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:585)
Producer Code: (using Spring-RabbitTemplate)
Student student = new Student();
student.setCompany("RLR");
student.setName("Pandi");
String jsonString = new ObjectMapper().unMarshall(student);
template.convertAndSend(jsonString);
Consumer Code:
String message = null;
delivery = consumer.nextDelivery(100);
if (delivery != null) {
message = new String(delivery.getBody());
}
ObjectMapper mapper = new ObjectMapper();
Student apiRequest = mapper.readValue(message, Student.class);
Upvotes: 2
Views: 2085
Reputation: 174739
I am not sure what your unMarshall()
method is, but I just tested with Jackson2 with no problems...
Foo foo = new Foo();
foo.setFoo("foo");
foo.setBar("bar");
String fooString = new ObjectMapper().writeValueAsString(foo);
template.convertAndSend("", "foo", fooString);
Channel channel = cf.createConnection().createChannel(false);
GetResponse response = channel.basicGet("foo", true);
String in = new String(response.getBody());
Foo fooIn = new ObjectMapper().readValue(in, Foo.class);
System.out.println(fooIn);
However, you simplify the sending side and the framework will take care of the conversion...
template.setMessageConverter(new Jackson2JsonMessageConverter());
template.convertAndSend("", "foo", foo);
response = channel.basicGet("foo", true);
in = new String(response.getBody());
fooIn = new ObjectMapper().readValue(in, Foo.class);
System.out.println(fooIn);
EDIT:
Just tested with Jackson 1 (codehaus) with no problems...
Foo foo = new Foo();
foo.setFoo("foo");
foo.setBar("bar");
String fooString = new ObjectMapper().writeValueAsString(foo);
template.convertAndSend("", "foo", fooString);
Channel channel = cf.createConnection().createChannel(false);
GetResponse response = channel.basicGet("foo", true);
String in = new String(response.getBody());
Foo fooIn = new ObjectMapper().readValue(in, Foo.class);
System.out.println(fooIn);
Upvotes: 2