2014-01-17 67 views
3

我在從EXTJS存儲區發送的JSON文件中反序列化多個任務(數據)時遇到問題。更新單個任務的工作正確。但是,當我們更新多個任務,它顯示無法將任務實例從START_ARRAY令牌反序列化

org.codehaus.jackson.map.JsonMappingException:無法反序列化bryntum.com.gnt.model.Task的情況下進行START_ARRAY令牌

我使用Spring MVC和Hibernate。 而我的Web控制器代碼是。

package bryntum.com.gnt.web; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 

import org.json.*; 

import bryntum.com.gnt.model.Task; 
import bryntum.com.gnt.model.TaskWrapper; 
import bryntum.com.gnt.service.TaskService; 
import bryntum.com.gnt.util.ReturnTasks; 


@Controller 
public class TaskController { 

    private TaskService taskService; 


    @RequestMapping(value="/tasks/update.action") 
    public @ResponseBody String update (@RequestBody TaskWrapper data) throws Exception { 
     try{ 
       TaskWrapper tw=(TaskWrapper)data; 
      List<Task> tasks = taskService.update(tw.getData()); 

      return ReturnTasks.mapOK(tasks); 
     } catch (Exception e) { 
      return ReturnTasks.mapError("Error trying to update task. "); 
     } 
    } 

    @Autowired 
    public void setEventService(TaskService taskService) { 
     this.taskService = taskService; 
    } 
} 

服務類代碼是..

package bryntum.com.gnt.service; 

import java.util.ArrayList; 
import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 

import bryntum.com.gnt.dao.TaskDAO; 
import bryntum.com.gnt.model.Task; 

@Service 
public class TaskService { 

    private TaskDAO taskDAO; 


    @Transactional 
    public List<Task> update(Task record){ 

     List<Task> returnTasks = new ArrayList<Task>(); 

     returnTasks.add(taskDAO.saveTask(record)); 

     return returnTasks; 
    } 


    @Autowired 
    public void setTaskDAO(TaskDAO taskDAO) { 
     this.taskDAO = taskDAO; 
    } 
} 

的Util類代碼,用於處理JSON ..

package bryntum.com.gnt.util; 

import java.io.IOException; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 

import org.springframework.stereotype.Component; 
import org.codehaus.jackson.JsonGenerationException; 
import org.codehaus.jackson.map.JsonMappingException; 
import org.codehaus.jackson.map.ObjectMapper; 
import org.json.*; 

import bryntum.com.gnt.model.Task; 
import org.codehaus.jackson.map.DeserializationConfig; 

@Component 
public class ReturnTasks { 

    private static JSONObject sortedByParentTasks = null; 
    private static JSONArray tasksTree = null; 
    private static JSONObject taskChildrenObject; 

    /** 
    * Generates JSON String to return in the modelAndView 
    * @param tasks 
    * @return 
    * @throws JSONException 
    * @throws IOException 
    * @throws JsonMappingException 
    * @throws JsonGenerationException 
    */ 

    public static String mapOK(List<Task> tasks) throws JSONException, JsonGenerationException, JsonMappingException, IOException{ 
     tasksTree = null; 
     tasksTree = new JSONArray(); 
     sortedByParentTasks = null; 
     sortedByParentTasks = new JSONObject(); 
     tasksTree = makeTree(tasks); 

     return tasksTree.toString(); 
    } 

    private static JSONArray makeTree(List<Task> list) throws JSONException, JsonGenerationException, JsonMappingException, IOException{ 

      ObjectMapper om= new ObjectMapper(); 
     Iterator<Task> listIterator = list.iterator(); 
     String parentId; 

     while(listIterator.hasNext()){ 

      om=om.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); 
      om.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY); 
      Task task = listIterator.next(); 
      JSONArray equalParentId; 
      parentId = ""+task.getParentId(); 
      String json = om.writeValueAsString(task); 
      JSONObject taskJSON = new JSONObject(json); 

      if (sortedByParentTasks.has(parentId)){ 
       sortedByParentTasks.accumulate(parentId, taskJSON); 
      } else { 
       equalParentId = new JSONArray(); 
       equalParentId.put(taskJSON); 
       sortedByParentTasks.put(parentId, equalParentId); 
      } 
     } 

     addNodes(sortedByParentTasks.getJSONArray("null")); 

     return tasksTree; 
    } 

    private static void addNodes(JSONArray nodes) throws JSONException{ 
     for(int i=0, l=nodes.length(); i<l; i++){ 
      taskChildrenObject = nodes.getJSONObject(i); 
      listHierarchy(taskChildrenObject); 
      tasksTree.put(taskChildrenObject); 
     } 
    } 

    private static void listHierarchy(JSONObject task) throws JSONException{ 
     JSONArray childrenArray = new JSONArray(); 
     JSONArray childNodes = new JSONArray(); 

     try { 
      childNodes = sortedByParentTasks.getJSONArray(""+task.get("Id")); 
     }catch(JSONException e){} 

     if (childNodes.length() > 0){ 
      for (int i=0, l=childNodes.length(); i<l; i++) { 
       JSONObject childObject = childNodes.getJSONObject(i); 
       childrenArray.put(childObject); 
       try{ 
        task.put("children", childrenArray); 
        task.put("leaf", false); 
       }catch(JSONException e){} 

       listHierarchy(childObject); 
      }   
     } 
    } 

    /** 
    * Generates modelMap to return in the modelAndView in case 
    * of exception 
    * @param msg message 
    * @return 
    */ 
    public static String mapError(String msg){ 

     Map<String,Object> modelMap = new HashMap<String,Object>(2); 
     modelMap.put("message", msg); 
     modelMap.put("success", false); 

     return modelMap.toString(); 
    } 
} 

模型類代碼..

package bryntum.com.gnt.model; 

import java.io.Serializable; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 

import org.codehaus.jackson.annotate.JsonAutoDetect; 
import org.codehaus.jackson.annotate.JsonProperty; 
import org.codehaus.jackson.annotate.JsonIgnoreProperties; 

@JsonAutoDetect 
@JsonIgnoreProperties(ignoreUnknown = true) 
@Entity 
@Table(name="events") 
public class Task implements Serializable { 

    @Id 
    @GeneratedValue 
    @Column(name="Id") 
    private int id; 

    @Column(name="Name", nullable=false) 
    private String name; 
    /*@Column(name="TASK", nullable=false) 
    private String task; 
    */ 
    @Column(name="StartDate", nullable=false) 
    private String startDate; 

    @Column(name="EndDate", nullable=false) 
    private String endDate; 

    @Column(name="parentId", nullable=true) 
    private Integer parentId; 
     /* 
     and more fields 
     */ 

     @JsonProperty("Id") 
    public int getId() { 
     return id; 
    } 

    @JsonProperty("Id") 
    public void setId(int id) { 
     this.id = id; 
    } 


    @JsonProperty("Name") 
    public String getName() { 
     return name; 
    } 

    @JsonProperty("Name") 
    public void setName(String name) { 
     this.name = name; 
    } 

    @JsonProperty("StartDate") 
    public String getStartDate() { 
     return startDate; 
    } 

    @JsonProperty("StartDate") 
    public void setStartDate(String start) { 
     this.startDate = start; 
    } 

    @JsonProperty("EndDate") 
    public String getEndDate() { 
     return endDate; 
    } 

    @JsonProperty("EndDate") 
    public void setEndDate(String end) { 
     this.endDate = end; 
    } 

    public Integer getParentId() { 
     return parentId; 
    } 

    public void setParentId(Integer id) { 
     this.parentId = id; 
    } 

} 

和一個包裝claas任務

package bryntum.com.gnt.model; 

import java.io.Serializable; 

public class TaskWrapper implements Serializable{ 

    private Task data; 

    public Task getData() { 
     return data; 
    } 

    public void setData(Task data) { 
     this.data = data; 
    } 

} 

回答

相關問題