How to return JSON data from spring Controller using @ResponseBody

后端 未结 8 1049
情深已故
情深已故 2020-11-27 04:55

Spring version 4.2.0, Hibernate 4.1.4 Here is my Controller function:

@RequestMapping(value = \"/mobile/getcomp\", method = Req         


        
相关标签:
8条回答
  • 2020-11-27 05:41

    You also need to be sure that returned bean is not empty (and can be serialized by 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. 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) )
    
    0 讨论(0)
  • 2020-11-27 05:42

    Considering @Arpit answer, for me it worked only when I add two jackson dependencies:

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

    Original answer that helped me is here: https://stackoverflow.com/a/33896080/3014866

    0 讨论(0)
提交回复
热议问题