简体   繁体   English

为什么 jaxb 不将此 XML 文档解组为 Java ZA8CFDE6331BD59EB66AC96F8911C?

[英]Why is jaxb not unmarshalling this XML document into a Java object?

Thank you for taking the time to read.感谢您花时间阅读。

Before asking I would like to point out that I've read as many similar posts on StackOverflow / the internet that I possibly can.在问之前,我想指出我已经阅读了尽可能多的 StackOverflow / 互联网上的类似帖子。

My goal is to deserialize the response from an API request into a usable java object.我的目标是将来自 API 请求的响应反序列化为可用的 java object。

I am sending an POST request to an endpoint to create a job in our schedule.我正在向端点发送一个 POST 请求,以在我们的计划中创建一个作业。 The job is created successfully and the following XML is returned in the body:作业创建成功,正文中返回以下 XML:

<entry xmlns="http://purl.org/atom/ns#">
    <id>0</id>
    <title>Job has been created.</title>
    <source>com.tidalsoft.framework.rpc.Result</source>
    <tes:result xmlns:tes="http://www.auto-schedule.com/client">
        <tes:message>Job has been created.</tes:message>
        <tes:objectid>42320</tes:objectid>
        <tes:id>0</tes:id>
        <tes:operation>CREATE</tes:operation>
        <tes:ok>true</tes:ok>
        <tes:objectname>Job</tes:objectname>
    </tes:result>
</entry>

However, when I try to unmarshal this into a POJO, the mapping is not working as expected.但是,当我尝试将其解组为 POJO 时,映射未按预期工作。

For the sake of simplicity, I am trying to capture only the first fields, id , title , and source (I have tried to capture only a single field -- id -- and I have also tried to do all fields to no avail).为了简单起见,我试图只捕获第一个字段idtitlesource (我试图只捕获一个字段—— id ——而且我也尝试过所有字段都无济于事) .

Here's what the POJO looks like:这是 POJO 的样子:

@XmlRootElement(name = "entry", namespace = "http://purl.org/atom/ns#")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {

    @XmlElement(name = "id")
    private String id;

    @XmlElement(name = "title")
    private String title;

    @XmlElement(name = "source")
    private String source;

    public Response() {}
}

In order to check whether or not the Xml elements are captured, I'm logging the attributes, which are null:为了检查 Xml 元素是否被捕获,我正在记录属性,它们是 null:

Response{id='null', title='null', source='null'}

Feign is the HTTP client sending out the requests, and here is the client file: Feign 是 HTTP 客户端发出请求,这里是客户端文件:

@FeignClient(name="ReportSchedulerClient", url = "https://scheduler.com", configuration = FeignClientConfiguration.class)
public interface ReportSchedulerClient {

    @PostMapping(value = "/webservice", consumes = "application/xml", produces = "text/xml")
    Response sendJobConfigRequest(@RequestBody Request request);

}

and a simple custom config file for auth:以及用于身份验证的简单自定义配置文件:

public class FeignClientConfiguration {
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("user", "pass");
    }
}

I am trying to avoid explicitly unmarshalling the file, but I have also tried to unmarshall the request explicitly using something like this:我试图避免显式解组文件,但我也尝试使用以下内容显式解组请求:

Response response = (Response) unmarshaller.unmarshal(new StreamSource(new StringReader(response.body().toString())));

Please let me know if you have any advice, if anything is wrong with my code, or any alternative suggestions.如果您有任何建议,如果我的代码有任何问题或任何替代建议,请告诉我。 Thanks in advance.提前致谢。

You need to specify the namespace at the element level.您需要在元素级别指定namespace For example:例如:

@XmlElement(name = "id", namespace = "http://purl.org/atom/ns#")
private String id;

To set a default namespace you can do it at the package level, creating the package-info.java file in the package folder with a content like this:要设置默认命名空间,您可以在 package 级别执行此操作,在 package 文件夹中创建 package-info.java 文件,其内容如下:

@XmlSchema(
    namespace = "http://purl.org/atom/ns#",
    elementFormDefault = XmlNsForm.QUALIFIED)
package your.model.package;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

Besides, as you are explicitly adding @XmlElement to all your fields, you can remove the @XmlAccessorType(XmlAccessType.FIELD) annotation, as it's purpose it's to map by default all fields to elements.此外,当您将@XmlElement显式添加到所有字段时,您可以删除@XmlAccessorType(XmlAccessType.FIELD)注释,因为它的目的是 map 默认所有字段到元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM