这是我的代表JSON的POJPO类
@JsonProperty("employee_id")
private String employeeId;
private String name;
// @JsonProperty("birth_date")
// //@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
// @JsonSerialize(using = LocalDateSerializer.class)
// private LocalDate birthDate;
public Employee(String employeeId, String name) {
super();
this.employeeId = employeeId;
this.name = name;
}
我的制作人课程。效果很好
@Autowired
private RabbitTemplate rabbitTemplate;
private ObjectMapper objectMapper = new ObjectMapper();
//@Scheduled(fixedRate=500)
public void sendHello (Employee emp ) throws JsonProcessingException
{
String json = objectMapper.writeValueAsString(emp);
rabbitTemplate.convertAndSend("course.employee", json );
}
我在RabbitMQ中的有效负载显示以下Json消息
headers:
content_encoding: UTF-8
content_type: text/plain
Employee info ->{"name":"Employee 0","employee_id":"emp 0"}
我的消费者阶层。这是造成问题的原因。
私有ObjectMapper objectMapper = new ObjectMapper();
@RabbitListener(queues="course.employee" )
public void listen( String message ) throws InterruptedException, JsonParseException, JsonMappingException, IOException
{
Employee emp ;
emp= objectMapper.readValue(message, Employee.class);
// Thread.sleep(ThreadLocalRandom.current().nextLong(2000));
}
下面是错误消息
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException:
Listener method 'public void
com.example.hibernatemapping.rabbitmqProducer.HelloRabbitConsumer.listen(java.lang.String)
throws java.lang.InterruptedException,com.fasterxml.jackson.core.JsonParseException,com.fasterxml.jackson.databind.JsonMappingException,java.io.IOException'
threw exception at
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.hibernatemapping.rabbitmqProducer.Employee` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"name":"Employee 4","employee_id":"emp 4"}"; line: 1, column: 2]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) ~[jackson-databind-2.9.8.jar:2.9.8]
答案 0 :(得分:1)
编辑问题后,情况与下面有所不同。同样,错误消息告诉您所有您需要了解的信息:
没有像默认构造那样的创造者存在
添加这样的构造函数:
public Employee() {
}
日志告诉您,您输入的是
Employee info ->{"name":"Employee 0","employee_id":"emp 0"}
这不是有效的JSON。
不要这样做:
rabbitTemplate.convertAndSend("course.employee","Employee info ->" + json );
执行以下操作:
rabbitTemplate.convertAndSend("course.employee", json);