code from http://zerockcode.blogspot.kr/


1. add maven repository library  

1
2
3
4
5
6
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.6</version>
        </dependency>
cs


2. write sample code sampleController5.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * how to create JSON data in Spring
 */
 
package org.zerock.web;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zerock.domain.ProductVO;
 
@Controller
public class SampleController5 {
 
    @RequestMapping("/doJSON"
    public @ResponseBody ProductVO doJSON() {     // use ResponseBody
        
        ProductVO prodVo = new ProductVO("sample"10000);
        
        return prodVo; // normal obj return
    }
}
 
cs


3. test 

input : http://localhost:8080/doJSON

result : {"name":"sample","price":10000.0}

+ Recent posts