Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 有没有比ListWrapper更好的方法来绑定列表<;T>;在SpringMVC方法中?_Spring Mvc - Fatal编程技术网

Spring mvc 有没有比ListWrapper更好的方法来绑定列表<;T>;在SpringMVC方法中?

Spring mvc 有没有比ListWrapper更好的方法来绑定列表<;T>;在SpringMVC方法中?,spring-mvc,Spring Mvc,为了在Spring MVC应用程序中检索列表,我想编写如下内容: public String myMethod(@RequestParam("foo") List<FooUi> foos) 我不喜欢这个解决方案,因为每次需要检索列表时,我都必须编写一个包装器。在本例中,包装器如下所示: public String myMethod(FooListWrapperUi fooListWrapperUi) @Data @JsonIgnoreProperties(ignoreUnknow

为了在Spring MVC应用程序中检索列表,我想编写如下内容:

public String myMethod(@RequestParam("foo") List<FooUi> foos)
我不喜欢这个解决方案,因为每次需要检索列表时,我都必须编写一个包装器。在本例中,包装器如下所示:

public String myMethod(FooListWrapperUi fooListWrapperUi)
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class FooListWrapperUi
{
    private ArrayList<FooUi> fooList; 
}
@数据
@JsonIgnoreProperties(ignoreUnknown=true)
公共类包装器UI
{
私人ArrayList傻瓜;
}
所以我的问题是,是否可以使用类似于第一个解决方案的东西,还是不可能,我需要编写一个包装器


谢谢。

您可以通过创建自己的
HandlerMethodArgumentResolver
来适应您的用例:

public class FooUiResolver implements HandlerMethodArgumentResolver {

    @Override 
    public boolean supportsParameter(MethodParameter methodParameter) {
        return (methodParameter.getParameterType().equals(FooUi.class) || 
            (methodParameter instanceof Collection<?> && ((ParameterizedType) methodParameter.getParameterType().getGenericSuperclass()).getActualTypeArguments()[0] == FooUi.class));
    }

    @Override 
    public Object resolveArgument(MethodParameter methodParameter,
        ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest,
        WebDataBinderFactory webDataBinderFactory) throws Exception {
        // Create instances of FooUi by accessing requests parameters in nativeWebRequest.getParameterMap()
    }
} 
注册后,您可以在控制器方法参数中使用
FooUi
,而无需
RequestParam
或任何其他注释:

@RequestMapping(value = "/foo")
public String myMethod(List<FooUi> foos){}
@RequestMapping(value=“/foo”)
公共字符串myMethod(列表foos){}

假设
FooUi
不是一个基本类型,您可能想创建一个定制的
HandlerMethodArgumentResolver
来告诉Spring如何从请求参数创建
FooUi
集合。我真的很惊讶在web上没有这样的例子。SpringMVC文档太大了,我找不到我需要的东西。有人已经做过类似的事情了吗?这里有相当基础的教程:。类似问题的各种答案,但我所知没有一个是针对您的特定用例的。实现起来应该足够简单,您只需要确定您的方法参数是单个对象还是集合。我没有提到,但我在我的项目中使用了Jackson。也许杰克逊天生就可以转换这个?看起来很不错!非常感谢。现在,它需要更加通用。我将搜索适用于任何类型的东西:FooUi、BarUi、AnythingUi……没有任何东西表明您不能让单个
HandlerMethodArgumentResolver
处理多个类型,您只需将逻辑编码到其中。
supportsParameter()。然后在
resolveArgument
方法中,由于Jackson库,它应该自动填充我的
集合。在这种情况下,您可能需要考虑编写自定义反序列化程序。这里有一个这样的例子:我很惊讶这没有在Spring框架中实现。我读过一篇文章,上面说,自从spring团队纠正了它之后,它本应该在本地工作。但是当我测试
公共字符串myMethod(@RequestMapping List foos)
时,我得到一个错误:“缺少必需的请求正文内容”
@RequestMapping(value = "/foo")
public String myMethod(List<FooUi> foos){}