我正在将jaxb对象发布到rest服务。生成的类没有xmlrootelement,因此我使用对象工厂createxml方法创建它。当我手动添加xmlrootelement时,它可以工作,但这只是一种解决方法,因为jaxb类总是在不使用xmlrootelement的情况下生成的。在发布请求时封送的XML似乎有一些问题。

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_XML);
    String userAndPass = "Test:Test123";
    headers.add("Authorization", "Basic " + Base64Utility.encode(userAndPass.getBytes()));

    JAXBElement<DocumentDef> documentDef = PrintFactory.createPrintObjects();

    HttpEntity<JAXBElement<DocumentDef>> request = new HttpEntity<>(documentDef, headers);

    MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
    map.add("lang", "2");

    ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request, String.class, map);

打印工厂.java
      public JAXBElement<DocumentDef> createPrintObjects() {

       DocumentDef documentDef = new DocumentDef();
       JAXBElement<DocumentDef> documentDefJAXBElement = factory.createXml(documentDef);
       return documentDefJAXBElement;
       }

对象工厂.java
        /**
 * Create an instance of {@link JAXBElement }{@code <}{@link DocumentDef }{@code >}}
 *
 */
@XmlElementDecl(namespace = "http://www.example.com/testservice", name = "xml")
public JAXBElement<DocumentDef> createXml(DocumentDef value) {
    return new JAXBElement<DocumentDef>(_Xml_QNAME, DocumentDef.class, null, value);
}

错误:
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [javax.xml.bind.JAXBElement] and content type [application/xml]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:859)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:617)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:588)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:507)

以下httpMessageConverter已注册:
org.springframework.http.converter.ByteArrayHttpMessageConverter@68022358, org.springframework.http.converter.StringHttpMessageConverter@7b3a8b9f, org.springframework.http.converter.StringHttpMessageConverter@645e9bc0, org.springframework.http.converter.ResourceHttpMessageConverter@7f438dba, org.springframework.http.converter.xml.SourceHttpMessageConverter@2c0def9c, org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@46ee015c, org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@2c833e50, org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@339b6365, org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1e9a965b

最佳答案

是的,这对你不起作用是因为:

JAXBElement<DocumentDef> documentDef = PrintFactory.createPrintObjects();
HttpEntity<JAXBElement<DocumentDef>> request = new HttpEntity<>(documentDef, headers);

同时Jaxb2RootElementHttpMessageConverter用于:
* <p>This converter can read classes annotated with {@link XmlRootElement} and
* {@link XmlType}, and write classes annotated with {@link XmlRootElement},
* or subclasses thereof.


public boolean canWrite(Class<?> clazz, MediaType mediaType) {
    return (AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && canWrite(mediaType));
}

当您的JAXBElement完全没有@XmlRootElement时。
试着找出没有包装的解决方案。

10-08 12:56