使用AngularJS

时间:2017-09-11 09:56:32

标签: java angularjs spring guava

具有此端点定义:

@RequestMapping(value = "/foo_resource", method = RequestMethod.GET)
public FooResponse retrieveFoo(
    @RequestParam("the_param") ImmutableSet<String> params,
    @RequestParam(name = "include_all", defaultValue = "true") boolean includeAll) {
        return aService.retrieveFoo(params, includeAll);
    }

我想让我的AngularJS控制器发送params,但是,我总是得到同样的错误:

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam com.google.common.collect.ImmutableSet<java.lang.String>] for value '"valuez"'; nested exception is java.lang.IllegalArgumentException: Could not instantiate Collection type: com.google.common.collect.ImmutableSet
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:43)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:203)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:173)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:108)
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47)
at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:713)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:120)
... 111 common frames omitted
Caused by: java.lang.IllegalArgumentException: Could not instantiate Collection type: com.google.common.collect.ImmutableSet
at org.springframework.core.CollectionFactory.createCollection(CollectionFactory.java:206)
at org.springframework.core.convert.support.StringToCollectionConverter.convert(StringToCollectionConverter.java:68)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:37)
... 118 common frames omitted

Angular方面,我当然尝试了几个选项,例如:

$http.get('xxx/foo_resource', {
      params: { 'the_param': JSON.stringify('valuez')}
})

或:

    $http.get('xxx/foo_resource', {
      params: { 'the_param[]': 'valuez'}
})

或:

    $http.get('xxx/foo_resource', {
      params: { 'the_param': ['valuez']}
})

...等。这番石榴有关系吗?我不能为此目的使用Immutable集合吗?还是我看不到什么?

BTW,我使用的是Java 8,AngularJS 1.6,Spring Boot 1.5.6,guava 22.0

1 个答案:

答案 0 :(得分:2)

Spring ArgumentResolver没有默认ImmutableSet个。 具有@RequestParam anotation的参数必须具有任何ArgumentResolver定义的或默认的构造函数。

选项: