使用FlexJson </enum>反序列化Java Set <enum>

时间:2014-04-24 08:40:25

标签: json enums deserialization spring-roo flexjson

我试图使用FlexJson反序列化Spring Roo控制器中的json对象,但是抛出异常,我无法解决它。

DaysOfWeek.java

public enum DaysOfWeek {

    SUNDAY("Sunday"),
    MONDAY("Monday"),
    TUESDAY("Tuesday"),
    WEDNESDAY("Wednesday"),
    THURSDAY("Thursday"),
    FRIDAY("Friday"),
    SATURDAY("Saturday");

    private final String description;

    DaysOfWeek(String description){
        this.description = description;
    }

    public String toString(){
        return description;
    }
}

CallDetail.java

public class CallDetail extends CalendarEvent {

    @NotNull
    private String title;

    @NotNull
    @Enumerated
    private CallFrequency frequency;

    @ElementCollection(targetClass = DaysOfWeek.class)
    @Enumerated
    private Set<DaysOfWeek> daysOfWeek;
}

CallDetailController.java

@RequestMapping(method=RequestMethod.POST, headers={"Accept=application/json"})
public ResponseEntity<String> createFromJson(@RequestBody String json) {
    CallDetail callDetail = null;

    try {
        callDetail = CallDetail.fromJsonToCallDetail(json); 
    } catch (Exception e) {
        e.printStackTrace();
    }
    .
    .
    .
}

JSP

//Create JSON Object containing basic CallDetail information
var callDetail = {
    "title" : callDetail_title.val(),
    "description" : callDetail_description.val(),
    "agent" : JSON.parse(callDetail_agent.val()),
    "allDay" : callDetail_isAllDayCall,
    "frequency" : callDetail_frequency,
    "tasks" : JSON.parse("[" + callDetail_tasks.find('option').map(function() { return $(this).val(); }).get().join(',') + "]")
};
callDetail.daysOfWeek = JSON.parse("[" + callDetail_weekdays.map(function() { return $(this).val(); }).get().join(',') + "]");
var jsonData = JSON.stringify(callDetail);

JSON OUTPUT daysOfWeek在发送到控制器的JSON对象中呈现如下:     {... “daysOfWeek”: “[周一,周二,周三]” ...}

调用fromJsonToCallDetail()方法时会抛出以下异常:

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum

任何人都可以帮助我,我只是想保存一组枚举?

0 个答案:

没有答案