'how can it solve correct converting to map from vo object in spring?
parameter VO in spring
public class Person {
private String name;
private int age;
private String job;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age= age;
}
public int getAge(){
return this.age;
}
public void setJob(String job){
this.job= job;
}
public String getJob(){
return this.job;
}
}
@Data
public class Developer extends Person{
private String lanaguage;
private int experience;
private List<Project> projects;
}
@Data
public class Project {
private String title;
private String period;
private String description;
private String framework;
private String memmonth;
}
JS
let params = {};
params.language = 'java';
params.experience = '10';
params.projects = [
{title: 'first-project', period: 1, description: 'operation', framework: 'N', memmonth: '3/1'}
,{title: 'second-project', period: 2, description: 'admin', framework: 'N', memmonth: '3/1'}
]
$.ajax({
contentType: 'application/json; charset=UTF-8'
, url: '/hr/insert/'
, data: JSON.stringify(param)
, type: 'POST'
, dataType: 'JSON'
, success: function (result) {
console.log(result);
}
, error: function(xhr) {}
});
Controller
@Controller
public class HumanResourceController {
private static final Logger logger = LoggerFactory.getLogger(HumanResource.class);
@Autowired
private ResponseEntity<?> HrServivceImple hrServiveImpl;
// hr insert
@RequestMapping(value="/hr/insert", method=RequestMethod.POST, produce="application/json;charaset=utf-8")
@ResponseBody
public ResponseEntity<?> insertResource(@RequestBody Developer params) {
// jackson library
ObjectMapper ob = new ObjectMapper();
// try object to map convert
logger.info("convert param {}", ob.convertValue(param, Map.class));
return hrServiceImpl.insert(params);
}
// hr update
@RequestMapping(value="/hr/update", method=RequestMethod.POST, produce="application/json;charaset=utf-8")
@ResponseBody
public ResponseEntity<?> updateResource(@RequestBody Developer params) {
// jackson library
ObjectMapper ob = new ObjectMapper();
// try object to map convert
logger.info("convert param {}", ob.convertValue(param, Map.class));
// update hr
return hrServiceImpl.update(params);
}
// hr delete
@RequestMapping(value="/hr/delete", method=RequestMethod.POST, produce="application/json;charaset=utf-8")
@ResponseBody
public ResponseEntity<?> updateResource(@RequestBody Developer params) {
// jackson library
ObjectMapper ob = new ObjectMapper();
// try object to map convert
logger.info("convert param {}", ob.convertValue(param, Map.class));
// delete hr
return hrServiceImpl.delete(params);
}
}
Log that convert vo to map in contrlloer side
{
language=java
, experience=10
, projects=[
{
name=null
, age=0
, job=null
, title=first-project
, period=1
, description=operation
, framework=N
, memmonth=3/1
}
, {
name=null
, age=0
, job=null
, title=second-project
, period=2
, description=admin
, framework=N
, memmonth=3/1
}
]
}
I was tried it code in spring. but I checked convert VO format that combined with variable in super class by using Jackson library. Why can it be included variable in super class to "proejcts" variable List? I want to get below it format
JSON format
{
language=java
, experience=10
, projects=[
{
title=first-project
, period=1
, description=operation
, framework=N
, memmonth=3/1
}
, {
, title=second-project
, period=2
, description=admin
, framework=N
, memmonth=3/1
}
]
, name=null
, age=0
, job=null
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
