Reputation: 25
I have some code who converts some xml to a java object. While it does that it always chooses to use the zero parameter constructor and so all information in the xml gets lost. What can I do about that?
My jaxb Java code
JAXBContext jaxbContext = JAXBContext.newInstance(PasswordResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
PasswordResponse pr = (PasswordResponse) jaxbUnmarshaller.unmarshal(**legit-xml-input**);
My Java Object code
package testhttprequest;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="PasswordResponse", namespace = "https://mynamespace.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class PasswordResponse
{
@XmlElement(name = "date", required = true)
public date date;
@XmlElement(name = "type", required = true)
public int type;
@XmlElement(name = "version", required = true)
public int version;
@XmlElement(name = "error", required = true)
public int error;
@XmlElement(name = "status", required = true)
public int status;
@XmlElement(name = "password", required = true)
public String password;
public PasswordResponse()
{
date = new date();
type = 1;
version = 1;
error = 1;
status = 1;
}
public PasswordResponse(int day, int month, int year, int type, int version)
{
this.date = new date(day, month, year);
this.type = type;
this.version = version;
this.error = 0;
this.status = 200;
}
public testhttprequest.date getDate()
{
return date;
}
public int getError()
{
return error;
}
public String getPassword()
{
return password;
}
public int getStatus()
{
return status;
}
public int getType()
{
return type;
}
public int getVersion()
{
return version;
}
public void setDate(testhttprequest.date date)
{
this.date = date;
}
public void setError(int error)
{
this.error = error;
}
public void setPassword(String password)
{
this.password = password;
}
public void setStatus(int status)
{
this.status = status;
}
public void setType(int type)
{
this.type = type;
}
public void setVersion(int version)
{
this.version = version;
}
}
and finally this is my xml:
<PasswordResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://mynamespace.com">
<date>
<day>1</day>
<month>1</month>
<year>1</year>
</date>
<type>1</type>
<version>1</version>
<error>0</error>
<status>200</status>
<password>000</password>
</PasswordResponse>
How can I force the application to use the Constructor with the parameters?
Upvotes: 1
Views: 1533
Reputation: 149027
While it does that it always chooses to use the zero parameter constructor and so all information in the xml gets lost.
Since you have a zero-arg constructor, and non-final fields or public accessors you are fine here and JAXB can use these to do everything you need it to. If you didn't have the zero arg constructor you would need to look into using an XmlAdapter
.
You are not mapping the namespace qualification correctly. You need to be sure you are properly mapping the namespace qualification. This can be done using the @XmlSchema‘ annotation on a class called
package-info`.
@XmlSchema(
namespace = "https://example.com",
elementFormDefault = XmlNsForm.QUALIFIED)
package testhttprequest;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
For More Information
I have written more about JAXB and namespace qualification on my blog:
Upvotes: 3