我在 Spring Boot 项目中使用 RabbitMQ:
sender
@Component
@AllArgsConstructor
public class UserSender {
private final RabbitTemplate rabbitTemplate;
public String send() {
User user = new User(1L, "Tom", "123");
rabbitTemplate.convertAndSend("userQueue", user);
return "user sender sent: " + user;
}
}
Receiver
@Component
public class UserReceiver {
@RabbitListener(queues = "userQueue")
@RabbitHandler
private void process(User user) {
System.out.println("received user: " + user);
}
}
启动时出现异常:
Caused by: java.lang.SecurityException: Attempt to deserialize unauthorized class com.example.lab06.entity.User; add allowed class name patterns to the message converter or, if you trust the message orginiator, set environment variable 'SPRING_AMQP_DESERIALIZATION_TRUST_ALL' or system property 'spring.amqp.deserialization.trust.all' to true
我检查了Spring AMPQ 文档
您可以使用这些转换器上的
属性来设置模式。或者,如果您信任所有消息发起者,则可以将环境变量allowedListPatterns
或系统属性SPRING_AMQP_DESERIALIZATION_TRUST_ALL
设置为spring.amqp.deserialization.trust.all
。true
但是,我无法在
spring.amqp.deserialization.trust.all
中设置application.properties
。我收到此错误:
Cannot resolve configuration property 'spring.amqp.deserialization.trust.all'
如何解决?
文档指出(强调我的):
...将 环境变量
或 系统属性SPRING_AMQP_DESERIALIZATION_TRUST_ALL
设置为spring.amqp.deserialization.trust.all
。true
application.properties
中配置的值不是环境变量或系统属性。环境变量在您的环境中设置,系统属性通常作为 -D
参数传递到 JVM。