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

61

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;
}

Pom.xml中的Jackson JSON映射器依赖项:

    <!-- Jackson JSON Mapper -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>

我从我的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)

链接 到我正在跟随的示例。


你能发一下你的Spring配置吗? - Rafik BELDI
@RafikBELDI 兄弟,一切都好。春季配置没能添加太多代码在问题中,因为它需要描述。 - Zahid Khan
11
我的问题是所有的getter都是私有的。 - Grim
谢谢,@peter。我的 getters 是包私有的,但它们需要是公共的。 - Dalbergia
8个回答

110

我遇到了同样的问题。由于我正在使用 @RestController,所以我没有使用 @ResponseBody。但是,由于我没有为 Company 类编写 getter/setter 方法,因此仍然会出现错误。所以,在添加了 getter/setter 后,我的问题得到了解决。


22
仅添加getter就足够了。 - M. Reza Nasirloo
6
这是正确的答案。不应添加更多依赖关系。 - H.Rabiee
谢谢,同样的方法对我也起作用了。但是我有点惊讶那真的很重要吗。 - Naga Srinu Kapusetti
1
对我有用。在我的情况下,将字段访问设置为public也起作用了。 - dorr
我尝试使用 Lombok 的 Getter 和 Setter,但是直到我添加了所有传统的Getter / Setter 才能正常工作。有人对此有什么想法吗? - Indrajeet Gour
显示剩余2条评论

106

在您的pom.xml文件中添加以下依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
</dependency>

7
让我一天都很开心,刚刚花了4个多小时,却没有人在任何地方提到这件事。 - Zahid Khan
3
添加依赖项是关键所在!谢谢! - Peter
3
依赖项对我也很关键。我想知道为什么他们不在Spring的页面上写下它们,以便操作:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html - Alfonso Nishikawa
3
对我也起作用了!给经过的用户一个小提示,始终选择使用依赖项的最新版本而不是此处提到的版本。不要使用 jackson 依赖的最新版本!我试过 2.7.0-rc1,但不起作用。对于 2.6.3 版本,可以正常工作。 - rmpt
5
对于spring4.x而言,只需添加getter/setter即可,不需要进行其他操作。 - thisdotnull
显示剩余8条评论

12

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

您还需要确保返回的 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) )

2
这更像是一条注释(而不是答案)。 - jogo

8

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

我正在使用Spring Boot版本2.0。


在这种情况下,添加getter和setter将修复问题。 - CodeIsLife

5

考虑到@Arpit的答案,对我而言只有在添加两个jackson依赖项后才有效:

<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>

当然,需要配置web.xml和<mvc:annotation-driven/>

帮助我的原始答案在这里:https://dev59.com/gFsX5IYBdhLWcg3wau8r#33896080


4

是的,只需添加使用public修饰符的setter/getter即可。


3

我正在使用groovy + springboot,遇到了这个错误。

如果我们使用以下依赖项,添加getter / setter就足够了。

implementation 'org.springframework.boot:spring-boot-starter-web'

随着Jackson核心类的使用。


0
在我的情况下,我使用的是与我需要使用的JDK 1.6不兼容的jackson-databind-2.8.8.jar,因此Spring无法加载此转换器。我降级了版本,现在它可以工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接