Back-End/Spring framework
Spring JSON 응답 보내기
@deveely
2018. 9. 17. 21:59
반응형
Spring 버전 : 4.3.4
1 2 3 4 5 6 7 8 9 10 | <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.0</version> </dependency> | cs |
위와 같이 dependency 추가 후 스프링 설정파일에
1 2 3 4 5 | <annotation-driven> <message-converters> <beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </message-converters> </annotation-driven> | cs |
위 내용 추가한 후
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Controller @RequestMapping("ajax") public class AjaxCallController { @RequestMapping(value="duplicate_check", method=RequestMethod.POST) @ResponseBody public HashMap<String, Object> duplicateCheck(@RequestParam String id) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("result", "result~~~"); return map; } } | cs |
컨트롤러에서 HashMap을 반환하면 해당 내용이 json으로 파싱되 전달된다.
반응형