spring mvc restful 传入List

来源:互联网 发布:python openwrt 模拟 编辑:程序博客网 时间:2024/06/05 04:08

 
如果传入的是List

需要在spring 配置文件添加converter

配置如下。

   <mvc:annotation-driven conversion-service="conversionService" />
   <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">       <property name="converters">           <list>               <bean class="com.test.BeanConverter"/>           </list>       </property>   </bean>


添加自定义的converter代码,可以使用alibaba.fastjson.已有的工具类。工具类依赖如下。

            <dependency>                <groupId>com.alibaba</groupId>                <artifactId>fastjson</artifactId>                <version>1.2.7</version>            </dependency>


java代码如下:

public class QuestionCustomerDtoConverter implements Converter<String , List<QuestionCustomerDto>>{@Overridepublic List<QuestionCustomerDto> convert(String source) {return JSONObject.parseArray(source, QuestionCustomerDto.class);}}