2

我在我的应用程序中使用 Spring MVC。对于某个 ajax 调用,我需要返回 JSON 响应。通常你会使用 @ReponseBody 注释来做到这一点。

这是我使用的一段代码。

@RequestMapping(value ="/getClients", headers="Accept=*/*", produces = "application/json")
public @ResponseBody List<Map<String, Object>> getClients(@ModelAttribute("searchText") String searchText, ModelMap model){
    logger.debug("Entering getClients with searchtext as : {}", searchText);
    List<Map<String, Object>> autoCompleterResult = null;
    try{
        autoCompleterResult = clientService.getClientsForAutoCompleter(searchText);
    logger.debug("Grid Result is : {}", autoCompleterResult );
    }catch(Exception e){
        logger.error("Exception occured", e);
    }
    return autoCompleterResult;
}

这适用于 Spring 3.1.4,但不适用于 Spring 3.2 版本。或者我需要做一些特定的配置才能使用 Spring 3.2?

MVC 配置

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven />
<mvc:resources location="/css/**" mapping="/css/" />
<mvc:resources location="/js/**" mapping="/js/" />
<mvc:resources location="/images/**" mapping="/images/" />
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.Exception">error</prop>
        </props>
    </property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="com.controllers" />
<context:property-placeholder location="classpath:application.properties"/>

PS:我已将 Jackson jar 包含在我的课程路径中。

更新 堆栈跟踪:

 02 Aug 2013 11:55:00 DEBUG impl.DistributionPlatformRepositoryImpl           (DistributionPlatformServiceImpl.java42) - Entering getPlatforms 
 02 Aug 2013 11:55:00 DEBUG impl.DistributionPlatformRepositoryImpl (DistributionPlatformRepositoryImpl.java40) - Entering getPlatforms
 02 Aug 2013 11:55:00 DEBUG impl.DistributionPlatformRepositoryImpl (DistributionPlatformServiceImpl.java88) - Entering getPlatformsAaData
 02 Aug 2013 11:55:00 DEBUG controllers.SearchPlatformController (SearchPlatformController.java56) - Grid Result is : {iTotalDisplayRecords=7, aaData=[[A, B, C, 7, D, E, C, In Active, 25], [A, T, B, 3, L, C, 001, In Active, 20]]}
 02 Aug 2013 11:55:00 DEBUG annotation.ExceptionHandlerExceptionResolver (AbstractHandlerExceptionResolver.java132) - Resolving exception from handler [public java.util.Map<java.lang.String, java.lang.Object> com.controllers.SearchPlatformController.getPlatforms(com.model.form.SearchPlatformForm,org.springframework.ui.ModelMap)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
 02 Aug 2013 11:55:00 DEBUG annotation.ResponseStatusExceptionResolver (AbstractHandlerExceptionResolver.java132) - Resolving exception from handler [public java.util.Map<java.lang.String, java.lang.Object> com.controllers.SearchPlatformController.getPlatforms(com.model.form.SearchPlatformForm,org.springframework.ui.ModelMap)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
 02 Aug 2013 11:55:00 DEBUG support.DefaultHandlerExceptionResolver (AbstractHandlerExceptionResolver.java132) - Resolving exception from handler [public java.util.Map<java.lang.String, java.lang.Object> com.controllers.SearchPlatformController.getPlatforms(com.model.form.SearchPlatformForm,org.springframework.ui.ModelMap)]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
 02 Aug 2013 11:55:00 DEBUG servlet.DispatcherServlet (DispatcherServlet.java999) - Null ModelAndView returned to DispatcherServlet with name 'managedaccounts': assuming HandlerAdapter completed request handling
 02 Aug 2013 11:55:00 DEBUG servlet.DispatcherServlet (FrameworkServlet.java951) - Successfully completed request
4

3 回答 3

3

最后经过大量搜索,我能够解决这个问题。多亏了 Stackoverflow。请参考以下链接:

https://stackoverflow.com/a/13939290/1061430

基本上以下部分需要包含在 MVC 配置文件中:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<!-- Turn off working out content type based on URL file extension, should fall back to looking at the Accept headers -->
<property name="favorPathExtension" value="false" />
</bean>

再次感谢stackoverflow。

于 2013-08-02T08:46:59.710 回答
1

只需将jackson jars 包含到您的项目中,spring 就会自动检测它的存在并使用它来序列化您的对象。

更新

我看到有人在没有说明原因的情况下否决了这个答案。这有点尴尬。特别是因为该陈述是正确的;)

所以这是从http://blog.springsource.org/2010/01/25/ajax-simplifications-in-spring-3-0/引用的一段

在幕后,Spring MVC 委托给 HttpMessageConverter 来执行序列化。在这种情况下,Spring MVC 调用一个建立在 Jackson JSON 处理器上的 MappingJacksonHttpMessageConverter。当您使用 mvc:annotation-driven 配置元素和 Jackson 存在于您的类路径中时,此实现会自动启用。

于 2013-08-01T11:17:08.493 回答
0

你用的是什么版本Jackson?Jackson 2.1.0 与我的 Spring 3.2 配合得很好,具有下一个 Maven 依赖项:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.1.0</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.1.0</version>
</dependency>
于 2013-08-01T15:41:09.497 回答