JavaScript 에서 보내는 데이터


 

var options = [];
options.push('1123');
options.push('1159');
options.push('1300');

var formdata = {
					"date" : 20211129,
					"address" : 'busan dongrae',
					"tel1" : '010',
					"tel2" : '1234',
					"tel3" : '1234',
					"times" : options,
			}
fetch(url, {
				method : "POST",
				headers : {
					"Content-Type" : "application/json",
				},
				body : JSON.stringify(formdata),
			}).then((response) => console.log(response));

 

보내지는 데이터 구조 ( Json 데이터 내부구조에 Array 형이 있는 경우가 많은데 여태 통째로 String 으로 받은다음 JsonParser API로 파싱해서 사용했었음)


{
      'date' : '20211129',
      'address' : 'busan dongrae',
      'tel1' : '010',
      'tel2' : '1234', 
      'tel3' : '1234',
      'times' : ['1123', '1159', '1300']
}

 

Spring 에서 받을 때, 보내주는 형태로 클래스를 설계하고,

주의할 점은 보내는 json key 값과 클래스의 변수명이 일치해야 자동으로 매핑


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class SendData {
	private String date;
	private String address;
	private String tel1;
	private String tel2;
	private String tel3;
	private String[] times;
	
	@Getter
	@Setter
	@ToString
	public static class Times {
		private String time;
	}
}

 

컨트롤러에서  @RequestBody ClassName Object 형태로 받을 수 있음


@ResponseBody
	@RequestMapping(value = "test", method = RequestMethod.POST)
	public String reservation(HttpSession session, @RequestBody SendData sendData)
			throws IOException {
            
            "";
	}