Javascript SPRING--如何向控制器提交对象数组

Javascript SPRING--如何向控制器提交对象数组,javascript,spring,spring-mvc,angularjs,Javascript,Spring,Spring Mvc,Angularjs,作为标题 我使用angularjs提交 我的spring控制器: @RequestParam(value = "hashtag[]") hashtag[] o 以上是数组参数的工作,但不是数组对象 我的js脚本: $http({ method: 'POST', url: url, data: $.param({ hashtag : [{label:"success",value:"ok"},{label:"s

作为标题

我使用angularjs提交

我的spring控制器:

@RequestParam(value = "hashtag[]") hashtag[] o
以上是数组参数的工作,但不是数组对象

我的js脚本:

$http({
         method: 'POST',
         url: url,
         data: $.param({
                hashtag : [{label:"success",value:"ok"},{label:"success",value:"ok"},{label:"success",value:"ok"}],
               }),
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' }  });
我从chrome上观察到,表单数据是

    hashtag[0][label]:success
    hashtag[0][value]:10

    hashtag[1][label]:success
    hashtag[2][value]:10

    hashtag[3][label]:success   
    hashtag[3][value]:10
但是春天告诉我

org.springframework.web.bind.MissingServletRequestParameterException: Required hashtag[] parameter 'id[]' is not present

以前,我能够接收参数数组,但不能接收对象。有人能启发我吗?

试试使用
@modeldattribute

创建一个新的Java类HashtagList,如下所示

public class HashTagList {

    private List<HashTag> hashTag;

    public List<HashTag> getHashTag() {
        return hashTag;
    }

    public void setHashTag(List<HashTag> hashTag) {
        this.hashTag = hashTag;
    }
}
java类名是hashtag还是hashtag?

试试@RequestParam(value=“hashtag”)hashtag[]o


假设您有一个名为hashtag havinf label and value attributes的类。

因为这是一个POST请求,所以您可以使用
@RequestBody
注释并创建一个DTO类来映射您发送的数据,甚至可以使用域对象。 例如,为什么不创建可重用的POJO类来保存键->值对,如:

@JsonPropertyOrder({"label", "value"})
public final class Pair<K,V> implements Map.Entry<K,V>, Serializable {
   private final K key;
   private final V value;

   @JsonCreator
   public Pair(@JsonProperty("label")K key, @JsonProperty("value")V value) {
       this.key = key;
       this.value = value;
   }
   // ... rest of the implementation
}

你能说得更具体些吗@RequestBody出现错误(org.springframework.web.HttpMediaTypeNotSupportedException:Content-type“application/x-www-form-urlencoded”不受支持),如果我没有指定('Content-type':'application/x-www-form-urlencoded'),则将没有表单数据。它是由表单数据生成的。Requestbody注释处理xml和json数据如果你想提交表单数据,你必须使用@ModelAttributeId,但只针对1个对象。对于数组中的多个对象(为了更容易理解,我编辑了示例代码),我没有办法弄清楚。
@JsonPropertyOrder({"label", "value"})
public final class Pair<K,V> implements Map.Entry<K,V>, Serializable {
   private final K key;
   private final V value;

   @JsonCreator
   public Pair(@JsonProperty("label")K key, @JsonProperty("value")V value) {
       this.key = key;
       this.value = value;
   }
   // ... rest of the implementation
}
public class HashTags implements Serializable {
    List<Pair<String, String>> hashtag = new ArrayList<>();
   // ... rest of the implementation
}
@RequestBody HashTags entity