Mam proste POJO uwagami klasę za pomocą JAXB tak:prosty sposób przypisać wartość domyślną z XMLELEMENT DefaultValue adnotacji
public class MyPojo
implements Serializable
{
private final static long serialVersionUID = 1234L;
@XmlElement(name = "Type", required = true, defaultValue = "none")
@NotNull
protected SeismicDataAcquisitionSystemType type;
@XmlElement(name = "IpAddress", required = true)
@NotNull
@Pattern(regexp = "((1?[0-9]?[0-9]|2[0-4]|[0-9]|25[0-5]).){3}(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])")
protected String ipAddress;
@XmlElement(name = "SealServerTcpPort", defaultValue = "1477")
@NotNull
protected int sealServerTcpPort;
@XmlElement(name = "PamServerTcpPort", defaultValue = "1485")
@NotNull
protected int pamServerTcpPort;
/**
* Obtient la valeur de la propriété type.
*
* @return
* possible object is
* {@link SeismicDataAcquisitionSystemType }
*
*/
public SeismicDataAcquisitionSystemType getType() {
return type;
}
/**
* Définit la valeur de la propriété type.
*
* @param value
* allowed object is
* {@link SeismicDataAcquisitionSystemType }
*
*/
public void setType(SeismicDataAcquisitionSystemType value) {
this.type = value;
}
public boolean isSetType() {
return (this.type!= null);
}
/**
* Obtient la valeur de la propriété ipAddress.
*
* @return
* possible object is
* {@link String }
*
*/
public String getIpAddress() {
return ipAddress;
}
/**
* Définit la valeur de la propriété ipAddress.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setIpAddress(String value) {
this.ipAddress = value;
}
public boolean isSetIpAddress() {
return (this.ipAddress!= null);
}
/**
* Obtient la valeur de la propriété sealServerTcpPort.
*
*/
public int getSealServerTcpPort() {
return sealServerTcpPort;
}
/**
* Définit la valeur de la propriété sealServerTcpPort.
*
*/
public void setSealServerTcpPort(int value) {
this.sealServerTcpPort = value;
}
public boolean isSetSealServerTcpPort() {
return true;
}
/**
* Obtient la valeur de la propriété pamServerTcpPort.
*
*/
public int getPamServerTcpPort() {
return pamServerTcpPort;
}
/**
* Définit la valeur de la propriété pamServerTcpPort.
*
*/
public void setPamServerTcpPort(int value) {
this.pamServerTcpPort = value;
}
}
próbuję zainicjować mojego POJO z domyślnej wartości jak tego
MyPojo myPojo = new MyPojo();
myPojo.getPamServerTcpPort(); // return 0
setDefaultValues(myPojo); // assign attributes with annotated default values
myPojo.getPamServerTcpPort(); // return 1485
Próbuję programowo za pomocą metody setDefaultValues (MyPojo loMyPojo), który analizuje klasę za pomocą interfejsu API java.lang.annotation i interfejsu API java.lang.reflect, ale mój kod jest brzydki i nie działa z moją własną wartością domyślną:
Muszę wspomnieć, że nie mogę zmodyfikować oryginalnej klasy MyPojo, ponieważ jest ona sama generowana przez XSD parsowanie przez JAXB
jakiś pomysł?
co ramy użyłeś aby wygenerować klasę (np Aksjomat 2)? – amphibient
Używam jaxb api z poleceniem xjc un a plugin (krasa) –