I have the following class which represents a POJO object
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
// Class to marshall and unmarshall the XML to POJO
// This is a class for the request XML
@XmlRootElement
public class KeyProvision {
private String Consumer ;
private String API ;
private String AllowedNames ;
public void setConsumer( String Consumer)
{
this.Consumer= Consumer;
}
public void setAPI( String API){
this.API = API;
}
public void setAllowedNames(String AllowedStoes){
this.AllowedNames = AllowedNames;
}
@XmlElement
public String getConsumer(){
return Consumer;
}
@XmlElement
public String getAPI(){
return API;
}
@XmlElement
public String getAllowedNames(){
return AllowedNames;
}
}
A function from my class to which requests are mapped
@POST
@Path("/request")
@Consumes(MediaType.APPLICATION_XML)
public Response getRequest(KeyProvision keyInfo){
/* StringReader reader = new StringReader(keyInfo); // this code just leads to an execution failure for some reason
try{
JAXBContext jaxbContext = JAXBContext.newInstance(KeyProvision.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
KeyProvision api = (KeyProvision) jaxbUnmarshaller.unmarshal(reader);
System.out.println(api);
} catch(JAXBException e){
e.printStackTrace();
}
*/
String result = "Track saved : " + keyInfo;
return Response.status(201).entity(result).build() ;
// return "success" ;
}
if I change
public Response getRequest(KeyProvision keyInfo)
to
public Response getRequest(String keyInfo)
I can see that the request is accepted but not stored as a POJO object .
if I leave it as public Response getRequest(KeyProvision keyInfo)
, I get a 400 error with the following message <u>The request sent by the client was syntactically incorrect.</u>
in my REST client when I try making the request.
this is my request body:
<?xml version="1.0" encoding="UTF-8"?>
<KeyProvision>
<Consumer> testConsumer </Consumer>
<API>posting</API>
<AllowedNames> google</AllowedNames>
</KeyProvision>
What am I missing here that is preventing a successfull Unmarshalling from XML to POJO
By JAXB defaults naming rules it will expect the element names to start with lower case. You will need to specify the names on @XmlRootElement
and @XmlElement
to match your document.
@XmlRootElement(name="KeyProvision")
public class KeyProvision {
And
@XmlElement(name="Consumer")
public String getConsumer(){
JAXB Debugging Trick
When unmarshal isn't working correctly try populating the object model and marshalling it out to see the expected document structure.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.