Spring Maven 프로젝트 만들기 - 6

Json 데이터를 생성하여 Response

1. pom.xml 에 json 관련 라이브러리 추가

<!-- JSON -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>

</dependency>

2. json 데이터 만들어 response 하기


@RequestMapping(value = "/getTime", method = RequestMethod.GET)
@ResponseBody
public JSONObject getTime(HttpServletRequest request, HttpServletResponse response) {
logger.info("Welcome home!");
JSONObject jsonResponse = new JSONObject();
String date = homeService.getTime();
int count = dao.selectTestCount();
logger.info(" count : " + count);
jsonResponse.put("serverTime", date);
return jsonResponse;
}


댓글