I need to map a PHP array to a Java bean.
This is my bean that map the form:
public class SearchModel{
private String id;
private String user;
private List<SearchRoom> rooms;
//get and set
}
Where SearchRoom is:
public class SearchRoom {
private int adults;
private int child;
private List<Integer> childrenAge;
//get and set
}
This is my Spring Controller:
@RequestMapping(value = "/search", method = RequestMethod.POST,headers="Accept=application/json")
public void search(@ModelAttribute SearchModel model) {
System.out.println(model.getRooms());
}
And this is what i'm trying to send using 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
)
I got this exception:
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)
What is a good solution to avoid that error and retrieve the correct values?
I solved converting php array into an xml string and making my controller accept xml and converting it into my bean using jaxb