2021. 11. 29. 10:26
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 {
"";
}
'Spring Framework > Spring' 카테고리의 다른 글
[Spring] WebClient 클래스로 API 요청 (0) | 2022.01.18 |
---|---|
[Spring Security] 사용자 로그인 상태 체크 (0) | 2022.01.18 |
[JAVA] Date 클래스(java.sql) -> Date.valueOf(String) (0) | 2021.09.30 |
[Web] Cookie (쿠키 생성, 읽기) 방법 (0) | 2021.07.20 |
[JSON] Java - json 파싱 (0) | 2021.07.13 |