简体   繁体   English

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

[英]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)

Link to the example I'm following. 链接到我正在关注的示例。

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.

相关问题 使用@Responsebody返回列表或从spring控制器返回ModelAndView哪个更好 - Returning list using @Responsebody or return ModelAndView from spring controller which one better Spring MVC @ResponseBody返回列表不适合使用hibernate的json响应 - Spring MVC @ResponseBody return list is not proper json reponse using hibernate @ResponseBody,ResponseEntity Spring将Object作为JSON返回 - @ResponseBody , ResponseEntity Spring return Object as JSON 如何转换ArrayList <String> 在Spring @ResponseBody中使用Jackson的JSON对象 - How to convert ArrayList<String> to JSON object using Jackson in Spring @ResponseBody 如何使用@ResponseBody从控制器获取String数组? - How to get String array from controller using @ResponseBody? 在Spring中使用ResponseBody注释返回Json无法正常工作 - Using ResponseBody annotation in Spring for returning a Json not working 从Spring控制器返回JSON数据类型/视图 - Return JSON data type/view from Spring controller 如何从 Spring MVC 3 Controller 返回字符串为 json? - How to return string as json from Spring MVC 3 Controller? 如何从 Spring Boot @Controller 返回 json/xml? - How to return json/xml from Spring Boot @Controller? How to pass JSON Object and return Object from Spring rest controller - How to pass JSON Object and return Object from Spring rest controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM