0

I am hitting post request and it's converting string to Jaxb.

As below class

@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
@JsonInclude(Include.NON_NULL)
public class Sample{
    @JsonProperty("name")
    private String name= null;

@JsonProperty("Number")
private String Number= null; 

and using the below method to convert as below

Public void setValue(Exchange exchange) throws InternalSystemException, JsonProcessingException, IOException, DataValidationException, JAXBException {
        
        MessageContentsList list = exchange.getIn().getBody(MessageContentsList.class);
        LOGGER.info("exchange message" +  exchange.getIn().getBody(String.class));
       exchange.getIn().setBody(list.get(0));
   
        String str = exchange.getIn().getBody(String.class);
        
        StringReader reader = new StringReader(new String(
                str.getBytes()));

        
        
        JAXBContext jaxbContext = JAXBContext
                .newInstance(Sample.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        Sample sample= (Sample ) jaxbUnmarshaller
                .unmarshal(reader);

I know issue is here:

Sample sample= (Sample ) jaxbUnmarshaller
        .unmarshal(reader);

But I could not find what is the exact issue.

javax.xml.bind.UnmarshalException: null
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:310) ~[jakarta.xml.bind-api-2.3.2.jar:2.3.2]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:548) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:234) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:199) ~[jaxb-impl-2.3.3.jar:2.3.3]

Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:204) ~[na:na]
    at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:178) ~[na:na]
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400) ~[na:na]
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327) ~[na:na]

Any help would be appreciated.

Amit Nijhawan
  • 303
  • 2
  • 10
  • 1
    Did you research your specific "Content is not allowed in prolog" error? For example, [here](https://stackoverflow.com/q/5138696/12567365) and any of [these](https://www.google.com/search?q=SAXParseException:+Content+is+not+allowed+in+prolog+site:stackoverflow.com)? If so, what were your findings? – andrewJames Jul 25 '22 at 13:09
  • Your JAXBContext instance is not bound to the `Sample` class. Try with `JAXBContext.newInstance(WOUpdateRequest.class, Sample.class);` – TacheDeChoco Jul 25 '22 at 13:25
  • @TacheDeChoco Only one class using Sample JAXBContext jaxbContext = JAXBContext .newInstance(Sample.class); – Amit Nijhawan Jul 25 '22 at 15:43
  • "_I already researched_" - OK - but there is no evidence of any research in your question, so we have to assume (rightly or wrongly) that you have not done any. – andrewJames Jul 25 '22 at 16:03
  • "_in my log I can not see any XML related issue_". It's right there in your question: `Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.` – andrewJames Jul 25 '22 at 16:03
  • @andrewJames I already researched , but in my log I can not see any XML related issue. I am converting XML request in below method : MessageContentsList list = exchange.getIn().getBody(MessageContentsList.class); LOGGER.info("exchange message" + exchange.getIn().getBody(String.class)); exchange.getIn().setBody(list.get(0)); String str = exchange.getIn().getBody(String.class); I have debugged the code and issue seems to be Sample sample= (Sample ) jaxbUnmarshaller .unmarshal(reader); – Amit Nijhawan Jul 25 '22 at 16:03
  • @andrewJames I agreed that XML issue, but in log it's not showing any line number for example XML reader error: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,2] Message: Reference is not allowed in prolog------------------------------ so how can we figure out what is the issue. – Amit Nijhawan Jul 25 '22 at 16:09
  • 1
    "_how can we figure out what is the issue_" - by researching all those other questions on Stack Overflow which describe the exact same error message as the one you are getting - and looking at all the different answers which provide possible solutions. See my first comment for the links. Am I missing something here? – andrewJames Jul 25 '22 at 16:12
  • Just for clarification - the "prolog" refers to the opening section of the XML file/document being processed. – andrewJames Jul 25 '22 at 16:33

0 Answers0