[英]How to return JSON data from spring Controller using @ResponseBody
Spring version 4.2.0, Hibernate 4.1.4 Here is my Controller
function: 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;
}
Jackson JSON mapper dependency in Pom.xml
: Pom.xml
Jackson JSON 映射器依赖Pom.xml
:
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
Getting the list in my ArrayList
, but when returning the following error is shown:在我的
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)
I was facing same issue.我面临同样的问题。 I did not put
@ResponseBody
since I was using @RestController
.我没有放置
@ResponseBody
因为我使用的是@RestController
。 But still I was getting error because I did not put the getter/setter
method for the Company class.但是我仍然收到错误,因为我没有为 Company 类放置
getter/setter
方法。 So after putting the getter/setter
my problem was resolved.所以在放置了
getter/setter
我的问题就解决了。
Add the below dependency to your pom.xml:将以下依赖项添加到您的 pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
You also need to be sure that returned bean is not empty (and can be serialized by Jackson).您还需要确保返回的 bean 不为空(并且可以由 Jackson 序列化)。 In my particular case I tried to return an instance of an object without getters and setters and without any jackson annotation and with fields equals to null.
在我的特殊情况下,我试图返回一个没有 getter 和 setter、没有任何 jackson 注释且字段等于 null 的对象实例。 I got following message:
我收到以下消息:
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) )
When I was facing this issue, I simply put just getter setter methods and my issues were resolved.当我遇到这个问题时,我只是简单地使用了 getter setter 方法,我的问题就解决了。
I am using Spring boot version 2.0.我正在使用 Spring Boot 2.0 版。
Considering @Arpit answer, for me it worked only when I add two jackson dependencies:考虑到@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>
and configured, of cause, web.xml <mvc:annotation-driven/>
.并配置了 web.xml
<mvc:annotation-driven/>
。
Original answer that helped me is here: https://stackoverflow.com/a/33896080/3014866帮助我的原始答案在这里: https : //stackoverflow.com/a/33896080/3014866
是的,只需添加带有 public 修饰符的 setter/getter ;)
I was using groovy+springboot and got this error.我正在使用 groovy+springboot 并收到此错误。
Adding getter/setter is enough if we are using below dependency.
如果我们使用以下依赖项,添加 getter/setter 就足够了。
implementation 'org.springframework.boot:spring-boot-starter-web'
As Jackson core classes come with it.由于杰克逊核心课程随之而来。
In my case I was using jackson-databind-2.8.8.jar
that is not compatible with JDK 1.6
I need to use so Spring wasn't loading this converter.就我而言,我使用的
jackson-databind-2.8.8.jar
与我需要使用的JDK 1.6
不兼容,因此 Spring 没有加载此转换器。 I downgraded the version and it works now.我降级了版本,现在可以使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.