Backend/JAVA

Response로 Json 생성

Mr.6_냥아치 2024. 2. 19. 18:00
반응형
@SuppressWarnings("unchecked")
@RequestMapping(value = "/sample/sample", produces="application/json; charset=utf-8")
@ResponseBody
public String selectMapData(HttpServletRequest request, HttpServletResponse response, 
	@RequestParam HashMap hMap, Model model, HttpSession session) throws IOException, Exception {

	JSONObject jsonObject = new JSONObject();

	try {
		List<DemoVO> DemoVOList = sampleService.selectSampleData(hMap);

		JSONArray jsonArray = new JSONArray();
		jsonObject.put("type", "FeatureCollection");
		jsonObject.put("name", "Demo");
			
		for(DemoVO DemoVO : DemoVOList) {
			JSONObject sampleObj = new JSONObject();
			JSONObject samplePropertiesObj = new JSONObject();
			samplePropertiesObj.put("SAMPLE_FULL_NAME", DemoVO.getSampleFullName());

			sampleObj.put("type", "Feature");
			sampleObj.put("properties", samplePropertiesObj);
			jsonArray.add(sampleObj);
		}

		jsonObject.put("features", jsonArray);

	} catch(SQLException e){
		logger.error("SQLException selectSampleData");
	} catch(IOException e){
		logger.error("IOException selectSampleData");			
	} catch(Exception e) {
		e.printStackTrace();
		logger.error("Exception selectSampleData!");
	}

	return jsonObject.toJSONString();
}

 

클라이언트 단에서 확인

 

 

F12에서 콘솔 탭으로 이동하여 뎁스 및 뎁스 이름 확인 가능

반응형