简体   繁体   中英

Error Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Employee': was expecting ('true', 'false' or 'null')

This is my POJPO class representing JSON

    @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;
    }

My producer class .It works fine

@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 );
}

My payload in RabbitMQ shows the following Json message

headers:    
content_encoding:   UTF-8
content_type:   text/plain
Employee info ->{"name":"Employee 0","employee_id":"emp 0"}

My consumer class.It is causing the issue.

private 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));
}

Error message is below

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]

After you've edited your question, the situation is different than below. Again, the error messages tells you all you need to know:

no Creators, like default construct, exist

Add such a constructor:

public Employee() {
}

As the log tells you, your input is

Employee info ->{"name":"Employee  0","employee_id":"emp 0"}

This is not valid JSON.

Don't do this:

rabbitTemplate.convertAndSend("course.employee","Employee info ->" + json );

Do this:

rabbitTemplate.convertAndSend("course.employee", json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM