클라이언트에서 json형태로 넘긴 데이터를 컨트롤러에서 String으로 받아 Json으로 변환한 뒤 Map으로 변환하여 사용하고싶었다.
 
코드 영화관 좌석을 체크하면 원하는 정보가 Object형태로 생성한 뒤 리스트에 담아서 form태그로 서버에 전송하고싶었다.
html태그에는 체크박스 클릭시 클릭이벤트를 달아준다.
1 <input  type ="checkbox"  name ="chkSeat"  value ="'+idx+'"  data-movieId ="17"  data-row ="F"  data-seatNo ="7"  onclick /> 
 
편의상 제이쿼리로 클릭이벤트를 달았다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 let  chkMovieSeatList = []; function  tickSeatCheckbox (event ) {	let  currentEl = event.currentTarget; 	 	     if (this .checked) { 		let  tickSeat = new  Object (); 		tickSeat.idx = $(this ).val(); 		tickSeat.movieId = $(this ).data('movieId' ); 		tickSeat.row = $(this ).data('row' ); 		tickSeat.seatNo = $(this ).data('seatNo' ); 		chkMovieSeatList.push(tickSeat) 	 	 } else  {  	 	chkMovieSeatList = chkMovieSeatList.filter((element )=> element.idx != $(currentEl).val());     } 	 	$("input[name=chkMovieSeatList]" ).val(JSON .stringify(chkMovieSeatJson)); } $(document ).ready(function ( ) { 	 	$("input[name=chkSeat]" ).change(function ( ) { 		tickSeatCheckbox();     }); }); 
 
전송은 form태그로 했다.
1 $("form[name=saveFrm]" ).submit(); 
 
1 2 3 4 5 public  class  Seat   {	private  int  id; 	 	private  String chkMovieSeatList;  } 
 
혹시 DTO vs Model vs Entity 이 세가지의 차이가 궁금하다면 여기를  클릭하면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @PostMapping ("/save" )public  String save (Seat param, RedirectAttributes redirectAttributes)   {	String redirect = "redirect:주소" ; 	BooleanAndMessageResult result = new  BooleanAndMessageResult(); 	try  { 		result = seatService.save(param); 		if  (result.isResult()) { 			redirectAttributes.addFlashAttribute("message" , "success" ); 		} else  { 			redirectAttributes.addFlashAttribute("message" , "fail" ); 		} 	} catch  (Exception e) { 		result.setResult(false ); 		result.setMessage("등록에 실패 하였습니다." ); 		redirectAttributes.addFlashAttribute("message" , "fail" ); 	} 	redirectAttributes.addFlashAttribute("result" , result); 	return  redirect; } 
 
gson을 이용하여 JSON을 HashMap으로 변환 String 형태로 받은 chkMovieSeatList을 Map형태로 변환한 뒤 체크된 갯수만큼 반복문을 돌려 db에 insert를 했다. 자바 JSON 라이브러리인 goolge.gson 를 이용해서 변환했다. chkMovieSeatList에서는 쌍따옴표가 "로 들어가 있기때문에 replaceAll()을 이용해서 변경해줬다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import  com.fasterxml.jackson.databind.JsonMappingException;import  com.fasterxml.jackson.databind.ObjectMapper;import  com.google.gson.JsonArray;import  com.google.gson.JsonElement;import  com.google.gson.JsonObject;import  com.google.gson.JsonParseException;import  com.google.gson.JsonParser;@Override @Transactional public  BooleanAndMessageResult save (Seat param)  {	BooleanAndMessageResult result = new  BooleanAndMessageResult(); 	 	 	 	JsonParser jsonParser = new  JsonParser(); 	JsonArray jsonArray = (JsonArray) jsonParser.parse(req.getChkMovieSeatList을().replaceAll(""" ,"\"" )); 	 	for (JsonElement seatEl : jsonArray){ 		JsonObject seatObj = (JsonObject) seatEl;	 		HashMap<String, Object> seatMap; 		try  { 			seatMap = new  ObjectMapper().readValue(seatObj.toString(), HashMap.class ) ; 			seatDAO.insertSeat(seatMap); 		} catch  (JsonParseException e) { 			 			e.printStackTrace(); 		} catch  (JsonMappingException e) { 			 			e.printStackTrace(); 		} catch  (IOException e) { 			 			e.printStackTrace(); 		} 	}	 }