jeudi 15 mai 2014

Java - « contenu n'est pas autorisé en prolog » lorsque JSON Unmarshalling - Stack Overflow


I'm trying to unmarshal some JSON using JAXB & the following test:


private static final String json = "{\"displayName\":\"2 KB\",\"externalId\":\"e1bc7db1-e7d5-41c9-8d2a-681f64ffea77\",\"contentType\":\"image/jpeg\",\"created\":\"2014-04-04T10:42:57\",\"lastModified\":\"2014-04-04T10:42:57\",\"originalFilename\":\"pgtips.jpg\",\"size\":\"2902\",\"tags\":\"\"}";

@Test
public void testDocStoreResponse() throws Exception {
JAXBContext context = JAXBContext.newInstance(JAXBFile.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader reader = new StringReader(json);
unmarshaller.unmarshal(reader);
}

JAXBFile is a simple POJO representing the JSON:


@XmlRootElement
public class JAXBFile extends AbstractBean implements IFile {
private String originalFilename;
private Date created;
private Date lastModified;
private String tags = "";
private String contentType;
private long size;

// Un-annotated getters & setters follow
}

But whenever I run it, I get a stacktrace starting with:


javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:578)

This used to be working so I've changed something but I have no idea what. Does anyone have any clues on what might be causing the problem? From a bit of debugging the classes in the stacktrace, it appears like the JSON is being parsed as if it's XML but I'm unsure why. Dependencies etc. seem to be fine.




The JAXB (JSR-222) is for unmarshalling XML not JSON. You are currently using the JAXB reference implementation (based on the stack trace). EclipseLink JAXB (MOXy) supports JSON-binding. Were you using that previously?




I'm trying to unmarshal some JSON using JAXB & the following test:


private static final String json = "{\"displayName\":\"2 KB\",\"externalId\":\"e1bc7db1-e7d5-41c9-8d2a-681f64ffea77\",\"contentType\":\"image/jpeg\",\"created\":\"2014-04-04T10:42:57\",\"lastModified\":\"2014-04-04T10:42:57\",\"originalFilename\":\"pgtips.jpg\",\"size\":\"2902\",\"tags\":\"\"}";

@Test
public void testDocStoreResponse() throws Exception {
JAXBContext context = JAXBContext.newInstance(JAXBFile.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader reader = new StringReader(json);
unmarshaller.unmarshal(reader);
}

JAXBFile is a simple POJO representing the JSON:


@XmlRootElement
public class JAXBFile extends AbstractBean implements IFile {
private String originalFilename;
private Date created;
private Date lastModified;
private String tags = "";
private String contentType;
private long size;

// Un-annotated getters & setters follow
}

But whenever I run it, I get a stacktrace starting with:


javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(Unknown Source)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:578)

This used to be working so I've changed something but I have no idea what. Does anyone have any clues on what might be causing the problem? From a bit of debugging the classes in the stacktrace, it appears like the JSON is being parsed as if it's XML but I'm unsure why. Dependencies etc. seem to be fine.



The JAXB (JSR-222) is for unmarshalling XML not JSON. You are currently using the JAXB reference implementation (based on the stack trace). EclipseLink JAXB (MOXy) supports JSON-binding. Were you using that previously?



0 commentaires:

Enregistrer un commentaire