如何使用包含@XmlElement 的 JAXB 注释将 XML 字符串反序列化为复合 Java 对象

问题描述

我有以下 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"])

我正在尝试为 post 请求创建一个模拟服务器,所以当我收到 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;

}

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

解决方法

我找到了解决方案。

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

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

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...