当前位置: 首页 > 知识库问答 >
问题:

使用ActiveMQ Artemis和Spring Boot“body不可分配给类”

郭皓
2023-03-14

我从Artemis和JMS开始,我有问题得到消息回来。制作者对信息的正确要求;即正确获取和回复。问题是在最后阶段,获取数据。

resp.setJMSType("com.nesdl.flyaway.core.modelmessages.ApplicationsEvent");
log.debug(resp.getJMSType() + " / " + resp.getBody(String.class));
ApplicationsEvent response = resp.getBody(ApplicationsEvent.class);
2021-06-02 09:39:40.060 DEBUG 14288 --- [p-nio-80-exec-1] c.n.f.f.s.ApplicationsServicesImpl       : com.nesdl.flyaway.core.modelmessages.ApplicationsEvent / is null : "{\"applicationsDto\":[{\"id\":1,\"version\":0,\"dateCreated\":\"2021-05-18T17:52:30+0000\",\"dateLastModified\":\"2021-05-18T17:52:30+0000\",\"uuid\":\"66ef5e19-e5ac-48d1-86c8-85d26e0fa205\",\"valid\":true,\"modified\":false,\"toHistorize\":false,\"name\":\"TestApp\",\"description\":\"Première application\"}]}"
2021-06-02 09:39:40.066 DEBUG 14288 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet        : Failed to complete request: javax.jms.MessageFormatException: Body not assignable to class com.nesdl.flyaway.core.modelmessages.ApplicationsEvent
2021-06-02 09:39:40.071 ERROR 14288 --- [p-nio-80-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.jms.MessageFormatException: Body not assignable to class com.nesdl.flyaway.core.modelmessages.ApplicationsEvent] with root cause

javax.jms.MessageFormatException: Body not assignable to class com.nesdl.flyaway.core.modelmessages.ApplicationsEvent
    at org.apache.activemq.artemis.jms.client.ActiveMQMessage.getBody(ActiveMQMessage.java:740) ~[artemis-jms-client-2.15.0.jar:2.15.0]
    at com.nesdl.flyaway.front.services.ApplicationsServicesImpl.getApplications(ApplicationsServicesImpl.java:52) ~[classes/:na]
    at com.nesdl.flyaway.front.services.ApplicationsServicesImpl$$FastClassBySpringCGLIB$$293cab08.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.6.jar:5.3.6]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) ~[spring-aop-5.3.6.jar:5.3.6]
@Override
public List<ApplicationDto> getApplications() throws IOException, JMSException {
    ApplicationsEvent mess = ApplicationsEvent.builder()
            .applicationsDto(new ArrayList<>())
            .build();
    log.debug("message sent :"+mess.toString());
    Message resp = jmsTemplate.sendAndReceive(JmsSystemConfig.APPLICATIONS_QUEUE, new MessageCreator() {
        @SneakyThrows
        @Override
        public Message createMessage(Session session) throws JMSException {
            Message appMess = session.createTextMessage(objectMapper.writeValueAsString(mess));
            appMess.setStringProperty("_type","com.nesdl.flyaway.core.modelmessages.ApplicationsEvent");
            return appMess;
        }
    });
    resp.setJMSType("com.nesdl.flyaway.core.modelmessages.ApplicationsEvent");
    log.debug(resp.getJMSType() + " / " + resp.getBody(String.class));
    ApplicationsEvent response = resp.getBody(ApplicationsEvent.class);
    log.debug(response.getApplicationsDto().toString());
    return response.getApplicationsDto();
}

我的消费者

@JmsListener(destination = JmsSystemConfig.APPLICATIONS_QUEUE)
public void replyApplications(@Payload ApplicationsEvent appsEvent, @Headers MessageHeaders headers, Message message) throws JMSException, JsonProcessingException {
    ApplicationsEvent resp = ApplicationsEvent.builder()
            .applicationsDto(as.getApplications())
            .build();
    jmsTemplate.convertAndSend(message.getJMSReplyTo(),objectMapper.writeValueAsString(resp));
    log.debug("reply ok "+resp.toString()+" / "+objectMapper.writeValueAsString(resp));
}

和JMS配置

@Configuration
public class JmsConfig {

    @Bean
    public MessageConverter jacksonJmsMessageConverter(ObjectMapper objectMapper) {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        converter.setObjectMapper(objectMapper);
        return converter;
    }
}

共有1个答案

桓嘉谊
2023-03-14

这里的基本问题是,您发送的是一条文本消息,但您试图使用javax.jms.message.getBody()将其直接分配给com.nesdl.flyaway.core.modelMessages.ApplicationSevent对象,这是行不通的。您必须将文本转换为此对象,就像使用ObjectMapper.WriteValueAsString将此对象转换为文本一样,例如:

ApplicationsEvent response = objectMapper.readValue(resp.getBody(String.class), ApplicationsEvent.class);

此外,这里没有理由调用javax.jms.message.setJMSType()。它本质上什么都不做,所以你只要移除它就很安全了。

 类似资料:
  • 棱角分明的医生说: 为什么?我使用 以下是我的代码版本:

  • 将以下代码转换为typescript时出错。 错误TS2339:属性样式在元素类型上不存在。 当我为如下相同的值分配所需值时 那么误差在元素上,那就是 错误TS2322:类型“Element | null”不可分配给类型“{style:any;}”。类型“null”不可分配给类型“{style:any;}”。

  • 我想使用而不使用。我们有一个与Spring现有的项目,但我有麻烦配置没有。 当我使用@EnableZuulProxy注释时,出现以下错误: 原因:org。springframework。豆。工厂UnsatifiedPendencyException:创建名为“org”的bean时出错。springframework。云netflix。祖尔。ZuulProxyConfiguration”:通过字段“

  • 问题内容: 输出:连接到数据库…输入要提取的余额: 20 net.ucanaccess.jdbc.UcanaccessSQLException:尝试分配给不可更新的列 问题答案: 检查访问数据库中的Atm对象,并确保它是表而不是查询。还要检查AccountBalance的数据类型,并确保它是可编辑字段。如果它是自动递增或计算的,则将无法更新。 编辑:看起来您必须声明它为可更新的游标。这是ucana

  • 这是一个类中的函数,允许你双击和单击手势。它在Swift 2.3中运行良好,但在转换到Swift 3后,它抛出了一些错误。我怎么也想不明白。我评论了它们发生的地方。

  • 我在使用我创建的bot为服务器中的不协调用户分配角色时遇到问题。 我编写的代码是用于与FaceIT交互的,它工作得很好,但我希望能够根据一个用户进行了多少次匹配来分配角色。 通过我的代码,我知道discord用户的ID,因为它们存储在配置文件中,配置被加载到一个名为 运行代码时,出现以下错误: 不一致外部命令。错误。CommandInvokeError:命令引发异常:AttributeError: