Я пытаюсь демаршировать данный XML в объект Java, заполняется все, кроме временной метки. Я написал собственный xmlAdapter для анализа временной метки, но во время отладки он никогда на этом не останавливается и не заполняет поле.
Example XML:
`"" +
"4" +
"628" +
"110080110" +
"1234" +
"" +
" " +
" 2016-10-21T12:56:19.549+05:30" +
"
1232131" +
"1231232" +
"251345" +
"7645233" +
"" +
" " +
" 1970-01-10 02:07:36.453" +
" 3425235" +
" 1" +
" 1" +
" 1234" +
" " +
" " +
" 1970-01-10 02:07:36.453" +
" 3425236" +
" 2" +
" 2" +
" 1235" +
" " +
"" +
"" +
"" +
"";
`
//classes in my project.
`@XmlRootElement(name = "RootElement")
public class ExampleClass {
@XmlElement(name = "Id")
protected String pId;
@XmlElement(name = "Number")
protected Integer cNumber;
protected Integer rId;
protected String orderNmbr;
@XmlElementWrapper(name = "responses")
@XmlElement(name = "Response")
protected List responses;
// Getters and setters
public class Response {
protected Date date;
protected BigDecimal premium;
protected BigDecimal income;
protected BigDecimal e1;
protected BigDecimal e2;
protected List tags;
// Getters and setters
public class Tag {
@XmlElement(name = "timeStamp", namespace = "http://www.w3.org/2001/XMLSchema-instance", type = String.class)
@XmlJavaTypeAdapter(CustomTimeStampAdapter.class)
protected Timestamp timeStamp;
protected Integer cd;
protected Integer cd1;
protected String valText;
protected String tokenId;
// Getters and setters
`
//here is my custom adapter.
`public class CustomTimeStampAdapter extends XmlAdapter {
/**
* Convert a value type to a bound type.
*
* @param timeStamp The value to be converted. Can be null.
* @throws Exception if there's an error during the conversion. The caller is responsible for
* reporting the error to the user through {@link ValidationEventHandler}.
*/
@Override
public Timestamp unmarshal(String timeStamp) throws Exception {
if (StringUtils.isBlank(timeStamp)) {
return null;
}
return Timestamp.valueOf(timeStamp.trim());
}
/**
* Convert a bound type to a value type.
*
* @param timeStamp The value to be convereted. Can be null.
* @throws Exception if there's an error during the conversion. The caller is responsible for
* reporting the error to the user through {@link ValidationEventHandler}.
*/
@Override
public String marshal(Timestamp timeStamp) throws Exception {
return timeStamp.toString();
}
}`
//bean for jaxb
`@Bean(name = "jaxb2Marshaller")
public Jaxb2Marshaller getJaxb2Marshaller() {
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setClassesToBeBound(
//all the classes the context needs to know about
ExampleClass.class, Response.class, Tag.class);
return jaxb2Marshaller;
}`
Все элементы заполняются, кроме «timeStamp». Что мне не хватает? Пожалуйста, помогите!
//here is my custom adapter. `public class CustomTimeStampAdapter extends XmlAdapter {
/** * Convert a value type to a bound type. * * @param timeStamp The value to be converted. Can be null. * @throws Exception if there's an error during the conversion. The caller is responsible for * reporting the error to the user through {@link ValidationEventHandler}. */ @Override public Timestamp unmarshal(String timeStamp) throws Exception { if (StringUtils.isBlank(timeStamp)) { return null; } return Timestamp.valueOf(timeStamp.trim()); }
/** * Convert a bound type to a value type. * * @param timeStamp The value to be convereted. Can be null. * @throws Exception if there's an error during the conversion. The caller is responsible for * reporting the error to the user through {@link ValidationEventHandler}. */ @Override public String marshal(Timestamp timeStamp) throws Exception { return timeStamp.toString(); } }`
//bean for jaxb `@Bean(name = "jaxb2Marshaller") public Jaxb2Marshaller getJaxb2Marshaller() { Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); jaxb2Marshaller.setClassesToBeBound( //all the classes the context needs to know about ExampleClass.class, Response.class, Tag.class);
return jaxb2Marshaller; }` [/code] Все элементы заполняются, кроме «timeStamp». Что мне не хватает? Пожалуйста, помогите!