org.springframework.http.converter.HttpMessageNotWritableException:找不到类型返回值的转换器:class java.util.ArrayList

时间:2018-05-25 11:30:54

标签: json ajax spring-mvc

我目前正在尝试从Spring MVC中的jsp实现ajax调用。 我的ajax电话如下:

$.ajax({
                type: 'POST',
                url: '/scrapingtoolweb/searchProcess',
                data : formData,
                dataType:"json",
                success: function(response) {
                    alert("success");
                    alert(response.status);
                    },
                    error: function(response) {  
                        alert("error");
                        alert(response.status);
                    }
                });

我的控制器代码如下:

@RequestMapping(value = "/searchProcess", method = RequestMethod.POST )
    @ResponseBody
    public List<WipoDataUI> search(@RequestParam String publicationDateFromUI ,@RequestParam String publicationDateToUI) {
    List<WipoDataUI> wipodata = new ArrayList<WipoDataUI>();
    wipodata = searchService.getData(resultData);
    return wipodata;

}

当我尝试将响应返回给jsp时,它会抛出500个异常

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpMessageNotWritable
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.ArrayList

我读了一些建议说要添加jackson绑定,我已经在我的servlet xml中添加了

<mvc:annotation-driven>
     <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
   </mvc:message-converters>
    </mvc:annotation-driven>

尽管它没有解决我的问题。有人可以就此问题提出建议吗?

2 个答案:

答案 0 :(得分:0)

我认为您不需要定义转换器,只需将以下依赖项添加到Author

pom.xml

在我的代码中,我为fastxml库提供了<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${fasterxml.version}</version> </dependency> 版本号。

答案 1 :(得分:0)

对我来说,它通过像这样定义转换器解决了这个问题:

  <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
      <list>
        <ref bean="jsonMessageConverter" />
      </list>
    </property>
  </bean>