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}
'문돌이의 IT > Spring' 카테고리의 다른 글
| 자바의정석 Chapter 4 반복문 while, do while (0) | 2017.08.17 |
|---|---|
| Uncaught ReferenceError: jQuery is not defined (0) | 2017.07.16 |
| [Spring] how to use redirect: (0) | 2017.02.04 |
| [Spring] how to use ModelAttribute (0) | 2017.02.03 |
| [Spring]Controller, annotation 기능 확인 (0) | 2016.12.21 |
