0

I have the following JAXB annotated classes:

    @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;

When I try and deserialize an input i am getting an error below:

Unrecognized field "Channel" (class a.request.Channels), not marked as ignorable (one known property: "channels"]) at [Source: (StringReader); line: 1, column: 136] (through reference chain: a.request.Channels["Channel"])

I am trying to create a mockserver for the post request so when i am getting request as JAXB xml format, i tried to use the mapper to read the request body and map it to the object.

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;

}

Can someone please explain how i can resolve it . I have tried many alternatives but seems like it didnt worked.

Stackflow
  • 1
  • 3

1 Answers1

0

I have found the solution.

To pass XML content, you need to wrap the content in a Reader, and unmarshal that instead:

JAXBContext jaxbContext = JAXBContext.newInstance(Channels.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

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

Stackflow
  • 1
  • 3