【问题标题】:How can I deserialize XML strings to composite Java object with JAXB annotations containing @XmlElement如何将 XML 字符串反序列化为使用包含 @XmlElement 的 JAXB 注释的复合 Java 对象
【发布时间】:2020-12-28 14:53:20
【问题描述】:

我有以下 JAXB 注释类:

    @Data
    @XmlAccessorType(XmlAccessType.NONE)
    @XmlRootElement(name="Channels", namespace="http://abc.channeltypes")
    public class Channels {
    
        @XmlElement(name = "Channel", required = true)
        private @Valid @NotNull List<Channel> channels;
}



@Data
@Getter
@XmlRootElement(name = "Channel")
@XmlAccessorType(XmlAccessType.NONE)
public class Channel {
    @XmlElement(name = "ChannelId")
    private Integer channelId;
    @XmlElement(name = "Name", required = true)
    private @NotEmpty String name;
    @XmlElement(name = "Type", required = true)
    private @NotEmpty String type;

当我尝试反序列化输入时,出现以下错误:

无法识别的字段“Channel”(a.request.Channels 类),未标记为可忽略(一个已知属性:“channels”]) 在 [来源:(StringReader);行:1,列:136](通过引用链:a.request.Channels["Channel"])

我正在尝试为发布请求创建一个模拟服务器,所以当我收到 JAXB xml 格式的请求时, 我尝试使用映射器读取请求正文并将其映射到对象。

mockServer.when(
                request()
                        .withPath("/[a-z]/[a-z]+")
                        .withMethod("POST")
                        .withHeader(
                                new Header("Content-Type", "application/xml"))

        )
                .respond(
                        httpRequest -> {
                            ChannelRef channelRef = new ChannelRef();
                            String body = httpRequest.getBodyAsString();
                            Channels channelsRequestBody =new XmlMapper().readValue(body,Channels.class);
                            channelsRequestBody.getChannels().forEach(channel -> {
                                Channel createdChannel = createChannel(channel);
                                channelRef.setChannelId(createdChannel.getChannelId());
                                channelRef.setExternalId(createdChannel.getExternalId());
                            });
                            Response response = getOkResponse(channelRef);
                            return  response()
                                    .withBody(
                                            new ObjectMapper()
                                                    .writeValueAsString(
                                                            response
                                                    )
                                    ).withStatusCode(201)
                                    .withContentType(MediaType.APPLICATION_JSON);
                        }
                );

private static Response getOkResponse(ChannelRef channelRef) {
Response response = new Response();
response.setResultCode(HttpStatus.OK.name());
response.setSystemTime(System.currentTimeMillis());
response.setResultObj(channelRef);

return response;

}

有人可以解释我如何解决它。 我尝试了很多替代方法,但似乎没有奏效。

【问题讨论】:

    标签: jackson jaxb jackson-databind jackson-dataformat-xml mockserver


    【解决方案1】:

    我找到了解决办法。

    要传递 XML 内容,您需要将内容包装在 Reader 中,然后将其解组:

    JAXBContext jaxbContext = JAXBContext.newInstance(Channels.class); 解组器 unmarshaller = jaxbContext.createUnmarshaller();

    StringReader reader = new StringReader(body); Channels a= (Channels) unmarshaller.unmarshal(reader);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多