如何使用@ResponseBody从spring Controller返回JSON数据

Zah*_*han 48 java spring json spring-mvc spring-4

Spring版本4.2.0,Hibernate 4.1.4 这是我的Controller功能:

@RequestMapping(value = "/mobile/getcomp", method = RequestMethod.GET)
@ResponseBody
public List<Company>  listforCompanies() {      
    List<Company> listOfCompanies= new ArrayList<Company>();        
    listOfCompanies = companyManager.getAllCompanies();
    return listOfCompanies;
}
Run Code Online (Sandbox Code Playgroud)

杰克逊JSON映射器依赖Pom.xml:

    <!-- Jackson JSON Mapper -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

获取列表ArrayList,但返回时会显示以下错误:

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/IrApp] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList] with root cause
    java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList
        at org.springframework.util.Assert.isTrue(Assert.java:68)
        at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:124)
Run Code Online (Sandbox Code Playgroud)

链接到我正在关注的示例.

Arp*_*wal 102

将以下依赖项添加到您的pom.xml:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • 让我的一天,只花了4个多小时,没有人甚至提到这个任何地方. (7认同)
  • 添加依赖项对我来说是关键!谢谢! (3认同)
  • 也为我工作!用户路过的一个小注释,总是选择使用最后一个版本的依赖项而不是这里提到的.不要使用最后版本的杰克逊依赖!我试过2.7.0-rc1并没有用.2.6.3工作得很好. (3认同)
  • spring4.x不是这种情况.只添加getter/setter就足够了. (3认同)
  • 家属也是我的关键.我想知道他们为什么不在Spring的页面中写下它们如何:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html (2认同)

Lyn*_*nAs 91

我面临同样的问题.@ResponseBody自从我使用以来我没有放过@RestController.但我仍然遇到错误,因为我没有把getter/setter方法用于公司类.所以在把getter/setter我的问题解决后.

  • 只添加吸气剂就足够了. (19认同)
  • 这是正确的答案.一个人不应该添加更多的依赖项. (5认同)

小智 9

您还需要确保返回的bean不为空(并且可以由Jackson序列化).在我的特殊情况下,我尝试返回一个没有getter和setter的对象实例,没有任何jackson注释,并且字段等于null.我得到以下消息:

com.fasterxml.jackson.databind.JsonMappingException:
    No serializer found for class com.foo.bar.Baz and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
Run Code Online (Sandbox Code Playgroud)

  • 这更像是评论(不是答案). (2认同)

小智 6

当我遇到这个问题时,我只是简单地使用getter setter方法,我的问题就解决了.

我使用的是Spring启动版2.0.


Rud*_*koŭ 5

考虑到@Arpit的回答,对我来说,只有当我添加两个杰克逊依赖项时它才有效:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

当然,还配置了 web.xml <mvc:annotation-driven/>

对我有帮助的原始答案在这里:https ://stackoverflow.com/a/33896080/3014866