如何解决Spring Boot具有JAXBElement <?>结果
在Jersey内,我可以执行以下模式:
return Response
.ok(objectFactory.createAboAntwort(new AboAntwortType()),APPLICATION_XML_TYPE)
.build();
当我尝试使用Spring Boot做同样的事情时,我能做的最好的就是使用下面的代码在每个函数中手动呈现XML文档,这非常难看。
StringWriter sw = new StringWriter();
DOMResult domResult = new DOMResult();
jaxb2Marshaller.marshal(objectFactory.createAboAntwort(new AboAntwortType()),domResult);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(domResult.getNode()),new StreamResult(sw));
return sw.toString();
当我刚刚返回成为JAXBElement时,已定义了默认的上下文类型。
return objectFactory.createAboAntwort(new AboAntwortType());
我收到警告,没有任何输出。
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class javax.xml.bind.JAXBElement] with preset Content-Type 'null']
我尝试添加WebConfig:
@Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
converters.add(marshallingMessageConverter());
}
@Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() {
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
converter.setMarshaller(jaxbMarshaller());
converter.setUnmarshaller(jaxbMarshaller());
return converter;
}
@Bean
public Jaxb2Marshaller jaxbMarshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("de.vdv453");
return marshaller;
}
我想我的主要问题是,没有XmlRootElement批注,默认设置不起作用。但是我不知道如何将正常工作的 Transform 插入。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。