gpt4 book ai didi

xml - 找不到适合请求类型 JAXBElement 的 HttpMessageConverter

转载 作者:数据小太阳 更新时间:2023-10-29 02:44:00 25 4
gpt4 key购买 nike

我正在将 JAXB 对象发布到 REST 服务。生成的类没有 XMLRootElement,因此我使用 Object Factory 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)

下面的 HttpMessageConverters 已经注册了:

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

最佳答案

我已经通过创建一个 .xjb 文件解决了这个问题,该文件在生成时自动将 XMLRootElement 注释附加到父 java 类,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
jxb:extensionBindingPrefixes="xjc" version="1.0">
<jxb:bindings schemaLocation="mySchema.xsd" node="/xs:schema">
<jxb:globalBindings>
<xjc:simple/>
</jxb:globalBindings>
</jxb:bindings>

关于xml - 找不到适合请求类型 JAXBElement 的 HttpMessageConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42761262/

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