将 Spring Boot JMS 应用程序配置为默认使用 JAXB 编组的最简单方法是什么?

Tod*_*one 4 java spring jms jaxb spring-jms

使用 Spring Boot REST 端点,似乎如果 JAXB 可用,只需传递“application/xml”的“Accept”标头就足以从一个非常简单的端点接收作为 XML 的输出,只要 @Xml...注释存在于实体上。

@RequestMapping(value = "/thing/{id}")
ResponseEntity<Thing> getThing(
        @PathVariable(value = "id") String id) {
    Thing thing = thingService.get(id)

    return new ResponseEntity<Thing>(thing, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)

但是,在调用时jmsTemplate.convertAndSend(destination, thing),我必须显式地将消息转换器插入到 JMS 模板中,其中包含以下代码

        JAXBContext context = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = context.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);

        TextMessage textMessage = session.createTextMessage(writer.toString());

        return textMessage;
Run Code Online (Sandbox Code Playgroud)

我当前正在使用带有注释和这些消息依赖项的 JavaConfig:

compile("org.springframework:spring-jms")
compile('org.springframework.integration:spring-integration-jms')
compile("org.apache.activemq:activemq-broker")
Run Code Online (Sandbox Code Playgroud)

还包括 Spring Boot 启动器中的这些内容,但不确定它们在这里是否重要。

compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-hateoas')
Run Code Online (Sandbox Code Playgroud)

我还使用 Groovy 和 Spock。

似乎肯定有某种方法可以在默认情况下无需代码即可完成此编组。建议?

Tod*_*one 5

我最终明确地从 Spring OXM 框架插入 Jaxb2Marshaller。我做的有点笨拙,因为我正在做 SpringBoot 和基于注释的配置,并且示例都是 XML。

@Autowired
JmsTemplate jmsTemplate

...

@Bean
MessageConverter messageConverter() {
    MarshallingMessageConverter converter = new MarshallingMessageConverter()
    converter.marshaller = marshaller()
    converter.unmarshaller = marshaller()
    // set this converter on the implicit Spring JMS template
    jmsTemplate.messageConverter = converter
    converter
}

@Bean
Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller()
    marshaller.classesToBeBound = [My.class, MyOther.class]
    marshaller
}
Run Code Online (Sandbox Code Playgroud)

我很想让变得更简单,但恐怕现在就只能这样了。