谢谢你抽出时间阅读。
在询问之前,我想指出,我已经在StackOverflow /互联网上阅读了尽可能多的类似文章。
我的目标是将响应从API请求反序列化为可用的java对象。
我正在向端点发送一个POST请求,以便在我们的计划中创建一个作业。成功地创建了作业,并在正文中返回了以下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>但是,当我试图将其解封为POJO时,映射并不像预期的那样工作。
为了简单起见,我只尝试捕获第一个字段:id、title和source (我尝试只捕获一个字段-- id --并且我还尝试执行allE 210字段,但没有结果)。
下面是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() {}
}为了检查是否捕获了Xml元素,我正在记录属性,这些属性为null:
Response{id='null', title='null', source='null'}冒充是发送请求的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);
}和一个用于auth的简单自定义配置文件:
public class FeignClientConfiguration {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "pass");
}
}我试图避免显式地解除文件的编组,但我也尝试使用以下内容显式地解除请求:
Response response = (Response) unmarshaller.unmarshal(new StreamSource(new StringReader(response.body().toString())));如果您有任何建议,如果我的代码有什么问题,或者其他建议,请告诉我。提前谢谢。
发布于 2021-01-05 22:28:18
您需要在元素级别指定namespace。例如:
@XmlElement(name = "id", namespace = "http://purl.org/atom/ns#")
private String id;若要设置默认命名空间,可以在包级别进行设置,在package文件夹中创建Packageinfo.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;此外,在向所有字段显式添加@XmlElement时,可以删除@XmlAccessorType(XmlAccessType.FIELD)注释,因为它的目的是在默认情况下将所有字段映射到元素。
https://stackoverflow.com/questions/65586599
复制相似问题