Spring RequestBody JSON List
Maven pom.xml
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.5.3</version>
</dependency>
JUnit RestTemplate Test
@Test
public void add() throws Exception{
String url = "http://localhost:8080/options/manager/add";
OptionsVO optionsVO = new OptionsVO();
optionsVO.setOpts_code( UUID.randomUUID().toString() );
optionsVO.setOpts_subject("test");
optionsVO.setOpts_memo("test");
String data_name = "opts_name";
String data_value = "뭐지???";
int data_type = 1;
String data_memo = "메모모모모.";
List<OptionsDataVO> optionsDataVOList = new ArrayList<OptionsDataVO>();
OptionsDataVO optionsDataVO = new OptionsDataVO();
optionsDataVO.setData_name(data_name);
optionsDataVO.setData_value(data_value);
optionsDataVO.setData_type(data_type);
optionsDataVO.setData_memo(data_memo);
optionsDataVOList.add(optionsDataVO);
optionsDataVO = new OptionsDataVO();
optionsDataVO.setData_name(data_name + "1");
optionsDataVO.setData_value(data_value);
optionsDataVO.setData_type(data_type);
optionsDataVO.setData_memo(data_memo);
optionsDataVOList.add(optionsDataVO);
optionsVO.setOptionsDataVO(optionsDataVOList);
ObjectMapper jackson = new ObjectMapper();
String params = jackson.writeValueAsString(optionsVO);
logger.info("=============================================\n");
logger.info(params);
HttpHeaders headers = new HttpHeaders();
headers.setContentType( MediaType.APPLICATION_JSON );
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
new HttpEntity( params, headers );
String data = restTemplate.postForObject(url, request, String.class);
logger.info(data);
}
Spring Controller
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public @ResponseBody OptionsVO add(@RequestBody OptionsVO optionsVO) {
return optionsVO;
}
Spring VO
public class OptionsVO {
private Integer opts_idx;
private String opts_code;
private String opts_subject;
private String opts_memo;
private List<OptionsDataVO> optionsDataVO;
public Integer getOpts_idx() {
return opts_idx;
}
public void setOpts_idx(Integer opts_idx) {
this.opts_idx = opts_idx;
}
public String getOpts_code() {
return opts_code;
}
public void setOpts_code(String opts_code) {
this.opts_code = opts_code;
}
public String getOpts_subject() {
return opts_subject;
}
public void setOpts_subject(String opts_subject) {
this.opts_subject = opts_subject;
}
public String getOpts_memo() {
return opts_memo;
}
public void setOpts_memo(String opts_memo) {
this.opts_memo = opts_memo;
}
public List<OptionsDataVO> getOptionsDataVO() {
return optionsDataVO;
}
public void setOptionsDataVO(List<OptionsDataVO> optionsDataVO) {
this.optionsDataVO = optionsDataVO;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
Spring VO
public class OptionsDataVO {
private Integer data_idx;
private Integer opts_idx;
private String data_name;
private String data_value;
private Integer data_type;
private String data_memo;
public Integer getData_idx() {
return data_idx;
}
public void setData_idx(Integer data_idx) {
this.data_idx = data_idx;
}
public Integer getOpts_idx() {
return opts_idx;
}
public void setOpts_idx(Integer opts_idx) {
this.opts_idx = opts_idx;
}
public String getData_name() {
return data_name;
}
public void setData_name(String data_name) {
this.data_name = data_name;
}
public String getData_value() {
return data_value;
}
public void setData_value(String data_value) {
this.data_value = data_value;
}
public Integer getData_type() {
return data_type;
}
public void setData_type(Integer data_type) {
this.data_type = data_type;
}
public String getData_memo() {
return data_memo;
}
public void setData_memo(String data_memo) {
this.data_memo = data_memo;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}