无法使用RabbitMQ构建`java.time.ZonedDateTime`的实例(不存在任何创建者,如默认构造一样)

问题描述 投票:1回答:1

我有三个spring应用程序,它们使用spring amqp(rabbitmq)相互通信:发票服务,跟踪服务和政府服务。我已经成功地从发票服务向跟踪服务发送了消息。

下面是来自跟踪服务的Receiver类。因此,我收到了来自发票服务的消息,在Handler类中做了一些事情之后,我向TrackingMessage服务发送了TrackingMessage。

@Component
public class Receiver {

    @Autowired
    private Handler handler;

    @Autowired
    private AmqpTemplate rabbitTemplate;

    static final String exchange = "exchange-invoice";
    static final String routingKey = "key-government";

    @RabbitListener(queues = "queue-tracking")
    public void receive(CustomMessage message) {
        List<TrackingMessage> trackingMessages = handler.getCoordinatesByDates(message);
        rabbitTemplate.convertAndSend(exchange,routingKey, trackingMessages.get(0));
    }
}

下面是MessageBrokerConfiguration类。我正在使用Jackson2Json将Java对象转换为JSON。

@Configuration
public class MessageBrokerConfiguration {

    static final String queueName = "queue-tracking";
    static final String exchange =  "exchange-invoice";
    static final String routingKey = "key-tracking";

    @Bean
    public Queue queue() {
        return new Queue(queueName);
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(exchange);
    }

    @Bean
    Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(routingKey);
    }

    @Bean
    Handler handler(){ return new Handler();}

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate;
    }
}

下面是来自跟踪服务的TrackingMessage类。

@Entity
public class TrackingMessage {

    @Id
    @GeneratedValue
    @JsonProperty("id")
    private int id;
    @JsonProperty("carId")
    private int carId;
    @JsonProperty("longitude")
    private double longitude;
    @JsonProperty("latitude")
    private double latitude;
    @JsonProperty("recordedTime")
    private ZonedDateTime recordedTime;

    public TrackingMessage() {
    }

    public TrackingMessage(int carId, double latitude, double longitude) {
        this.carId = carId;
        this.latitude = latitude;
        this.longitude = longitude;
        this.recordedTime = getTimeNow();
    }

    public TrackingMessage(@JsonProperty("id") int carId, @JsonProperty("carId") double latitude, @JsonProperty("longitude") double longitude,
                           @JsonProperty("recordedTime") ZonedDateTime recordedTime) {
        this.carId = carId;
        this.latitude = latitude;
        this.longitude = longitude;
        this.recordedTime = recordedTime;
    }

    public int getCarId() {
        return carId;
    }

    public void setCarId(int carId) {
        this.carId = carId;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public ZonedDateTime getRecordedTime() {
        return recordedTime;
    }

    public void setRecordedTime(ZonedDateTime recordedTime) {
        this.recordedTime = recordedTime;
    }

    private ZonedDateTime getTimeNow() {
        return ZonedDateTime.now(ZoneOffset.UTC);
    }
}

下面是政府服务的接收器类。因此,当我在此接收器类中收到TrackingMessage时,会出现以下错误:

无法构造java.time.ZonedDateTime的实例(不存在像默认构造一样的创建者)

我的来自政府服务的MessageBrokerConfiguration和TrackingMessage类看起来与跟踪服务中的类相同。有人的想法吗?

@Component
public class Receiver {
    @RabbitListener(queues = "queue-government")
    public void receive(TrackingMessage message) {
        System.out.println(message);
    }
}
json spring-boot serialization rabbitmq deserialization
1个回答
0
投票
@Bean public MessageConverter jsonMessageConverter(ObjectMapper mapper) { return new Jackson2JsonMessageConverter(mapper); } @Bean public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) { final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); ObjectMapper mapper = new ObjectMapper() .registerModule(new ParameterNamesModule()) .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule()); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.setDateFormat(new StdDateFormat()); rabbitTemplate.setMessageConverter(jsonMessageConverter(mapper)); return rabbitTemplate; }
© www.soinside.com 2019 - 2024. All rights reserved.