【问题标题】:how to serialize a json date time to joda-time如何将json日期时间序列化为joda-time
【发布时间】:2016-09-20 04:30:36
【问题描述】:

这是我在从客户端解析有效负载时遇到的异常 无法从字符串值('2014-06-18 06:26:56-07:00')实例化类型 [简单类型,类 org.joda.time.DateTime] 的值;没有单字符串构造函数/工厂方法

因为对于接受字符串日期的 joda-time 没有争议

这是来自(客户端/网络)、模型和控制器的我的 json 负载

有效载荷

[
  {
    "id": 10000000,
    "name": "Dimebad Darrel",
    "duetime": "2014-06-18 06:26:56-07:00",
    "jointime": "2015-04-08 12:47:16-07:00"
  }
]

客户类

@Entity
public class Customer implements Serializable, Comparable<Customer>{

    @Id
    private long id;

    private String name;

    @JsonFormat(pattern = "yyyy-MM-ddTHH:mm:ss")
    //@JsonSerialize(using = DateSerializer.class)
    private DateTime duetime;

    @JsonFormat(pattern = "yyyy-MM-ddTHH:mm:ss")
    //@JsonSerialize(using = DateSerializer.class)
    private DateTime jointime;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public DateTime getDuetime() {
        return duetime;
    }

    public void setDuetime(DateTime duetime) {
        // String startDate = "2013-07-12T18:31:01.000Z";
        // DateTime dt = ISODateTimeFormat.dateTime().parseDateTime(startDate); 
        this.duetime = duetime;
    }

    public DateTime getJointime() {
        return jointime;
    }

    public void setJointime(DateTime jointime) {
        this.jointime = jointime;
    }

    @Override
    public int compareTo(Customer customer) {
        DateTime thisDueDate = customer.getDuetime();
        DateTime otherDueDate = customer.getDuetime();

        if( thisDueDate.isAfter(otherDueDate) )
            return 1;
        else if( thisDueDate.isBefore(otherDueDate) )
            return -1;
        // default equals   
        return 0;
    }

}

CustomerController.class

public class CustomerController extends Controller{

    @Inject
    CustomerService service;

    public Result sortCustomer() {
        try{
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

            Json.setObjectMapper(objectMapper);
            String jsonString = request().body().asJson().toString();
            List<Customer> customers = objectMapper.readValue(jsonString,
                            objectMapper.getTypeFactory().constructCollectionType(List.class, Customer.class));

            //service.sort( customers );

            return ok( Json.toJson(customers) );
        }catch(Exception ex ){
            throw new IllegalStateException(ex.getMessage(), ex);
        }
    }
}

【问题讨论】:

    标签: java json playframework jodatime


    【解决方案1】:

    试试jackson-datatype-joda

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JodaModule());
    

    【讨论】:

    • 配置映射器注册模块,这就是我所缺少的!谢谢!
    猜你喜欢
    • 2017-11-28
    • 1970-01-01
    • 2013-08-18
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    相关资源
    最近更新 更多