알쓸전컴(알아두면 쓸모있는 전자 컴퓨터)

axios(post) -> spring framework json 객체 변환 본문

Web /Spring Framework tip

axios(post) -> spring framework json 객체 변환

백곳 2018. 3. 3. 10:39

axios(post) -> spring framework json 객체 변환 


Controller 에 추가시 


@RequestMapping(value = "/suggestion_peer_insert", method = RequestMethod.POST)

public void suggestion_peer_insert(@RequestBody String filterJSON,HttpServletRequest request, HttpServletResponse response) {

try {

suggestion_dao.suggestion_peer_insert(filterJSON,request, response);

}catch(Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}


@RequestBody String filterJSON 을 추가해 줍니다 .


그리고 json 에서 보낼때 


axios.post('/W_Server/suggestion_peer_update',{
doc_number : this.prop_doc_numer,
peer_name : this.prop_peer_name,
point : this.total_point,
comment : this.prop_comment,
money_effect : this.money_effect_select,
creater : this.prop_creater_select,
expandability : this.prop_expandability_select,
manager : this.prop_manager_select

}).then((Response) => {
}).catch(ex=>{

})



실제 로직 구현에서는 미리 json 형식의 객체 형식을 만들어 줘야 합니다. 


public class suggestion_peer_borad_obj {

String idsuggestion_peer_borad;

String doc_number;

String peer_name;

String point;

String comment;

String money_effect;

String creater;

String expandability;

String manager;

public String getIdsuggestion_peer_borad() {

return idsuggestion_peer_borad;

}

public void setIdsuggestion_peer_borad(String idsuggestion_peer_borad) {

this.idsuggestion_peer_borad = idsuggestion_peer_borad;

}  


public String getDoc_number() {

return doc_number;

}  

public void setDoc_number(String doc_number) {

this.doc_number = doc_number;

}

public String getPeer_name() {  

return peer_name;

}

public void setPeer_name(String peer_name) {

this.peer_name = peer_name;

}

public String getPoint() {

return point;

}

public void setPoint(String point) {

this.point = point;

}

public String getComment() {

return comment;

}

public void setComment(String comment) {

this.comment = comment;

}

public String getMoney_effect() {

return money_effect;

}

public void setMoney_effect(String money_effect) {

this.money_effect = money_effect;

}

public String getCreater() {

return creater;

}

public void setCreater(String creater) {

this.creater = creater;

}

public String getExpandability() {

return expandability;

}

public void setExpandability(String expandability) {

this.expandability = expandability;

}

public String getManager() {

return manager;

}

public void setManager(String manager) {

this.manager = manager;

}

}


pom.xml 에  아래 라이브러리를 추가해 주세요 


<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.5.1</version>

</dependency>

<dependency>

    <groupId>com.googlecode.json-simple</groupId>

<artifactId>json-simple</artifactId>

<version>1.1</version>

</dependency>



그리고 실제 로직 부분에서 아래와 같이 코딩 하면  json 으로 받은 데이터가 객체로 변하게 됩니다.



public void suggestion_peer_insert(String filterJSON, HttpServletRequest request, HttpServletResponse response)

throws Exception {

// TODO Auto-generated method stub

ObjectMapper objectMapper = new ObjectMapper();

suggestion_peer_borad_obj borad_obj = objectMapper.readValue(filterJSON, suggestion_peer_borad_obj.class);

}


Comments