Spring: @ModelAttribute and array php


Spring: @ModelAttribute and array php

我需要将一个PHP数组映射到一个Java bean。

这是我的bean映射的形式:

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
}

这是我的弹簧控制器:

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

这是我试图用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)

什么是一个好的解决方案,以避免错误和检索正确的值?

PHP和JAVA?这真的可能吗?和. .你为什么要这么做?

我认为这是不可能的,因为Java和PHP是不同的技术。

错误无效属性'rooms[0][adults]告诉你它找不到"rooms"数组。这很正常,java页面中没有声明数组但在php中

我解决了将php数组转换为xml字符串并使控制器接受xml并使用jaxb将其转换为bean的问题