2014-05-21 76 views
1

我需要將一個PHP數組映射到一個Java bean。Spring:@ModelAttribute和數組php

這是我的豆映射形式:

public class SearchModel{ 
    private String id; 
    private String user; 
    private List<SearchRoom> rooms; 

    //get and set 
} 

其中SearchRoom是:

public class SearchRoom { 
    private int adults; 
    private int child; 
    private List<Integer> childrenAge; 

    //get and set 
} 

這是我的春天控制器:

@RequestMapping(value = "/search", method = RequestMethod.POST,headers="Accept=application/json") 
public void search(@ModelAttribute SearchModel model) { 
    System.out.println(model.getRooms()); 
} 

而這就是我」 m試圖使用PHP發送:

Array 
(
    [id] => xxx 
    [rooms] => Array 
     (
      [0] => Array 
       (
        [adults] => 3 
        [child] => 2 
        [childrenAges] => Array 
         (
          [0] => 2 
          [1] => 5 
         ) 

       ) 

      [1] => Array 
       (
        [adults] => 2 
        [child] => 0 
        [childrenAges] => Array 
         (
         ) 

       ) 

      [2] => Array 
       (
        [adults] => 2 
        [child] => 4 
        [childrenAges] => Array 
         (
          [0] => 1 
          [1] => 4 
          [2] => 12 
          [3] => 17 
         ) 

       ) 

     ) 

    [user] => yyy 
) 

我得到這個異常:

Invalid property 'rooms[0][adults]' of bean class [com.giove.viaggi.hsw.models.SearchModel]: Property referenced in indexed property path 'rooms[0][adults]' is neither an array nor a List nor a Map; returned value was [3]] with root cause 
org.springframework.beans.InvalidPropertyException: Invalid property 'rooms[0][adults]' of bean class [com.giove.viaggi.hsw.models.SearchModel]: Property referenced in indexed property path 'rooms[0][adults]' is neither an array nor a List nor a Map; returned value was [3] 
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1046) 
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:922) 
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:82) 
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:728) 
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:624) 
    at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:189) 

什麼是一個很好的解決方案,以避免錯誤和檢索正確的價值觀?

回答

0

我解決了轉換PHP數組轉換成XML字符串,使我的控制器接受XML,並用它轉化成我的豆JAXB

相關問題