How I can send json response from my spring controller?

前端 未结 2 919
长发绾君心
长发绾君心 2021-01-17 04:44

I\'m a newbie in the theme. How can I return JSON data from my controller, using something like that (using ResponseBody)

@RequestMapping(value = \"/ajaxtest         


        
相关标签:
2条回答
  • 2021-01-17 05:31

    If you are using maven, you can include the following dependency in your pom.xml

    Jackson Mapper Version 1 which is good enough to convert your object to JSON object:

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    

    Or you can also go for latest Jackson version,

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.0</version>
    </dependency>
    
    0 讨论(0)
  • 2021-01-17 05:38

    For Converting to json request you must include following 3 jar to your project build path. Jackson jar is use for convrting HTTP request to jason format.

    Also mention headear=content-type=application/json

    These are the jar files

    jackson-mapper-asl.jar
    jackson-core-asl.jar
    jackson-jaxrs.jar
    
    0 讨论(0)
提交回复
热议问题