Java 如何使用@ResponseBody 从 spring Controller 返回 JSON 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32905917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return JSON data from spring Controller using @ResponseBody
提问by Zahid Khan
Spring version 4.2.0, Hibernate 4.1.4Here 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;
}
Hymanson JSON mapper dependency in Pom.xml
:
Hymanson JSON 映射器依赖项Pom.xml
:
<!-- Hymanson JSON Mapper -->
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-mapper-asl</artifactId>
<version>${Hymanson.version}</version>
</dependency>
Getting the list in my ArrayList
, but when returning the following error is shown:
在 my 中获取列表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)
Linkto the example I'm following.
链接到我正在关注的示例。
采纳答案by Arpit Aggarwal
Add the below dependency to your pom.xml:
将以下依赖项添加到您的 pom.xml:
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.5.0</version>
</dependency>
回答by ashirman
You also need to be sure that returned bean is not empty (and can be serialized by Hymanson). In my particular case I tried to return an instance of an object without getters and setters and without any Hymanson annotation and with fields equals to null. I got following message:
您还需要确保返回的 bean 不为空(并且可以由 Hymanson 序列化)。在我的特殊情况下,我尝试返回一个没有 getter 和 setter、没有任何 Hymanson 注释且字段等于 null 的对象实例。我收到以下消息:
com.fasterxml.Hymanson.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) )
回答by LynAs
I was facing same issue. I did not put @ResponseBody
since I was using @RestController
. But still I was getting error because I did not put the getter/setter
method for the Company class. So after putting the getter/setter
my problem was resolved.
我面临同样的问题。我没有放,@ResponseBody
因为我正在使用@RestController
. 但是我仍然收到错误,因为我没有getter/setter
为 Company 类放置方法。所以在把getter/setter
我的问题解决后。
回答by Rudzianko?
Considering @Arpit answer, for me it worked only when I add two Hymanson dependencies:
考虑到@Arpit 的回答,对我来说,它只有在我添加两个 Hymanson 依赖项时才有效:
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-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
回答by arvind-dhariwal
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 版。
回答by IsidroGH
In my case I was using Hymanson-databind-2.8.8.jar
that is not compatible with JDK 1.6
I need to use so Spring wasn't loading this converter. I downgraded the version and it works now.
在我的情况下,我使用的Hymanson-databind-2.8.8.jar
与JDK 1.6
我需要使用的不兼容,所以 Spring 没有加载这个转换器。我降级了版本,现在可以使用了。
回答by Arvind Kumar
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 Hymanson core classes come with it.
由于Hyman逊核心课程随之而来。
回答by ovgu12
Yes just add the setters/getters with public modifier ;)
是的,只需添加带有 public 修饰符的 setter/getter ;)