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)
Lyn*_*nAs 91
我面临同样的问题.@ResponseBody自从我使用以来我没有放过@RestController.但我仍然遇到错误,因为我没有把getter/setter方法用于公司类.所以在把getter/setter我的问题解决后.
小智 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)
考虑到@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
| 归档时间: |
|
| 查看次数: |
86960 次 |
| 最近记录: |