简体   繁体   English

使用 JAXB 解组日期但出现空指针异常

[英]Unmarshalling dates using JAXB but getting nullpointer exception

I am trying to convert XML to java object. In my xml, there is field which looks like:我正在尝试将 XML 转换为 java object。在我的 xml 中,有一个字段如下所示:

<pickDisplayTs>2021-09-24T18:03:06.603 +0000</pickDisplayTs>

My Java object looks like the following:我的 Java object 如下所示:

@XmlElement(name = "pickDisplayTs" )
@XmlJavaTypeAdapter(DateAdapter.class)
public Date pickDisplayTs;

My DataAdapter class is the following:我的 DataAdapter class 如下:

    import javax.xml.bind.annotation.adapters.XmlAdapter;
    import java.text.SimpleDateFormat;
    import java.util.Date;

   public class DateAdapter extends XmlAdapter<String, Date> {

   private static final String CUSTOM_FORMAT_STRING = "yyyy-MM- 
   dd'T'HH:mm:ss.SSS'Z'";


    @Override
    public Date unmarshal(String v) throws Exception {
    return new SimpleDateFormat(CUSTOM_FORMAT_STRING).parse(v);
   }

@Override
public String marshal(Date v) throws Exception {
    return new SimpleDateFormat(CUSTOM_FORMAT_STRING).format(v);
}

} }

Code reference: https://github.com/eugenp/tutorials/blob/950bbadc353bdca114befc98cf4a18476352220e/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/DateAdapter.java代码参考: https://github.com/eugenp/tutorials/blob/950bbadc353bdca114befc98cf4a18476352220e/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/DateAdapter.java

This is the method for unmarshalling the xml file:这是解组 xml 文件的方法:

  String filepath = "xml/PickComplete.xml";
  String xmlPickComplete = readFromResources(filepath);
  PickComp pickCompleteMq = Xml.xmlToObject(xmlPickComplete, PickingSubSystemOrderCompleteMessage.class);

The entire pickCompleteMq is coming to be null but if I am declaring the pickDisplayTs as string, its all good, not sure where I am going wrong.整个 pickCompleteMq 将变为 null,但如果我将 pickDisplayTs 声明为字符串,一切都很好,但不确定我哪里出错了。 But I need the field to be in Date.但我需要该字段在日期中。

Any help will be appreciated.任何帮助将不胜感激。 Thank you.谢谢你。

The problem is with the input.问题出在输入上。 XML extract you have provided doesn't follow the DateAdapter you are using.您提供的 XML 摘录不符合您使用的 DateAdapter。 If you marshal a pojo that contain Date the expected xml tag should be如果您封送包含 Date 的 pojo,则预期的 xml 标签应该是

<pickDisplayTs>2022-02-16T14:02:13.010Z</pickDisplayTs>

Trying to parse the given input gives ParseException.尝试解析给定的输入会产生 ParseException。 Code snippet:代码片段:

Date parse = new SimpleDateFormat(CUSTOM_FORMAT_STRING).parse("2021-09-24T18:03:06.603 +0000");
System.out.println(parse);

Output Output

java.text.ParseException: Unparseable date: "2021-09-24T18:03:06.603 +0000" java.text.ParseException:无法解析的日期:“2021-09-24T18:03:06.603 +0000”
at java.text.DateFormat.parse(DateFormat.java:366)在 java.text.DateFormat.parse(DateFormat.java:366)

Solution proposal:解决方案建议:

    private static final String CUSTOM_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss.SSS Z";
    public static void main(String[] args) throws ParseException {
        String dateStr = "2021-09-24T18:03:06.603 +0000";
        Date marshaledDate = new SimpleDateFormat(CUSTOM_FORMAT_STRING).parse(dateStr);

        SimpleDateFormat format = new SimpleDateFormat(CUSTOM_FORMAT_STRING);
        format.setTimeZone(TimeZone.getTimeZone("UTC"));

        String unmarshalledDate = format.format(marshaledDate);
        System.out.println(unmarshalledDate);
    }

You can use the above logic in your DataAdapter class as follows:您可以在您的 DataAdapter class 中使用上述逻辑,如下所示:

public class DateAdapter extends XmlAdapter<String, Date> {
        private static final String CUSTOM_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss.SSS Z";

        @Override
        public Date unmarshal(String v) throws Exception {
            return new SimpleDateFormat(CUSTOM_FORMAT_STRING).parse(v);
        }

        @Override
        public String marshal(Date v) throws Exception {
            SimpleDateFormat sdf = new SimpleDateFormat(CUSTOM_FORMAT_STRING);
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            return sdf.format(v);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM