gpt4 book ai didi

spring - 消息转换异常 : Failed to resolve class name in Spring AMQP

转载 作者:行者123 更新时间:2023-12-04 12:50:43 33 4
gpt4 key购买 nike

我正在尝试使用带有 jackson2JsonMessageConverter 的 Spring AMQP 来简单发送和接收消息.
还有,_TypeId_的意义是什么?这里
为什么它显示带有类名的发件人包?
我在接收消息时遇到问题。

下面是我的配置

org.springframework.amqp.support.converter.MessageConversionException: failed to resolve class name. Class not found [org.springframework.amqp.helloworld.User] at org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper.getClassIdType(DefaultJackson2JavaTypeMapper.java:121) at org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper.toJavaType(DefaultJackson2JavaTypeMapper.java:90) at org.springframework.amqp.support.converter.Jackson2JsonMessageConverter.fromMessage(Jackson2JsonMessageConverter.java:145) at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.extractMessage(AbstractAdaptableMessageListener.java:236) at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:288) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:777) ... 10 common frames omitted Caused by: java.lang.ClassNotFoundException: org.springframework.amqp.helloworld.User at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139) at org.springframework.util.ClassUtils.forName(ClassUtils.java:250) at org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper.getClassIdType(DefaultJackson2JavaTypeMapper.java:118) ... 15 common frames omitted



XML 配置
            <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">


<rabbit:connection-factory id="connectionFactory"
channel-cache-size="25" host="10.165.18.29" username="BipUser"
password="bip" />

<rabbit:queue name="Job Queue"></rabbit:queue>

<rabbit:queue name="Input Queue"></rabbit:queue>

<rabbit:queue name="More Info Queue"></rabbit:queue>

<rabbit:queue name="Adaptor O/P Queue"></rabbit:queue>

<rabbit:queue name="Command Queue"></rabbit:queue>

<rabbit:queue name="Error Queue"></rabbit:queue>

<bean id="simpleMessageConverter"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter">
</bean>

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
message-converter="jsonConverterWithDefaultType" />

<rabbit:listener-container
connection-factory="connectionFactory" auto-declare="true"
message-converter="simpleMessageConverter" auto-startup="true"
acknowledge="auto">
<rabbit:listener ref="rabbitMQJobListener"
queue-names="Job Queue" priority="10" />

</rabbit:listener-container>

<rabbit:admin connection-factory="connectionFactory" id="amqpAdmin" />

<bean id="rabbitMQJobListener" class="com.bosch.bip.rabbitmq.consumer.RabbitMQJobListener">
</bean>

<rabbit:annotation-driven container-factory="rabbitListenerContainerFactory" />

<bean id="rabbitListenerContainerFactory"
class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="messageConverter" ref="jsonConverterWithDefaultType"></property>
</bean>

<bean id="jsonConverterWithDefaultType"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter">
<property name="classMapper">
<bean class="org.springframework.amqp.support.converter.DefaultClassMapper">
</bean>
</property>
</bean>
</beans>

发件人
package org.springframework.amqp.helloworld;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class Sender {


public static void main(String[] args) {

ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
User user=new User();
user.setPassword("welcome");
user.setUserName("welcome");
user.setXml("myxml");
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);

Jackson2JsonMessageConverter converter = context.getBean(Jackson2JsonMessageConverter.class);
MessageProperties properties = new MessageProperties();
properties.setHeader("user", "user");
properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
Message message = converter.toMessage(user, properties);

System.out.println(message);



rabbitTemplate.send(message);
}

/* @RabbitListener(queues = HelloWorldConfiguration.helloWorldQueueName)
public void handleMessage(User user) {
System.out.println("User Values::::::::"+user.getPassword());
}*/
}

消费者
package com.bip.rabbitmq.consumer;

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.bip.entity.User;

@EnableRabbit
@Component
public class RabbitMQJobListener {


@RabbitListener(queues="Job Queue")
public void onMessage(User message) {
System.out.println(new String(message.getPassword()));

}
}

兔MQ
Exchange    (AMQP default)
Routing Key Job Queue
Redelivered ○
Properties
priority: 0
delivery_mode: 2
headers:
user: user
__TypeId__: org.springframework.amqp.helloworld.User
content_encoding: UTF-8
content_type: application/json
Payload
57 bytes
Encoding: string
{"userName":"welcome","password":"welcome","xml":"myxml"}

最佳答案

_TypeID_ header 设置在出站上,以告诉入站将 JSON 转换为哪个类。如果要转换为与 JSON 类型兼容的其他类,则必须配置转换器。

如果它总是相同的类,请使用自定义 ClassMapper (不是默认的)。

或者,请参阅 this test ,其 listenerits configuration查看如何配置不同的 typeid 映射。

关于spring - 消息转换异常 : Failed to resolve class name in Spring AMQP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39137746/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com