서비스 측이 json 데이터를 받아들여 여러 대상으로 전환하는 두 가지 처리
2182 단어 백엔드
![@RequestMapping(value="/handle_user.do", method=RequestMethod.POST)
@ResponseBody
public ResponseResult handleUser(@RequestBody Map param){
MultiParams params = new MultiParams();
params.setUser(JSONObject.parseObject(JSONObject.toJSONString(param.get("user")), User.class));
params.setCar(JSONObject.parseObject(JSONObject.toJSONString(param.get("car")), UserCar.class));
params.setWork(JSONObject.parseObject(JSONObject.toJSONString(param.get("work")), UserWork.class));
params.setRelationship(JSONObject.parseObject(JSONObject.toJSONString(param.get("relationship")), UserRelationship.class));
params.setColleague(JSONObject.parseObject(JSONObject.toJSONString(param.get("colleague")), UserColleague.class));
params.setFood(JSONObject.parseObject(JSONObject.toJSONString(param.get("food")), UserFood.class));
params.setHobby(JSONObject.parseObject(JSONObject.toJSONString(param.get("hobby")), UserHobby.class));
User user = (User) getEntity("user", User.class);
UserCar car = (UserCar) getEntity("car", UserCar.class);
UserColleague colleague = (UserColleague) getEntity("colleague", UserColleague.class);
UserFood food = (UserFood) getEntity("food", UserFood.class);
UserHobby hobby = (UserHobby) getEntity("hobby", UserHobby.class);
UserRelationship relationship = (UserRelationship) getEntity("relationship", UserRelationship.class);
UserWork work = (UserWork) getEntity("work", UserWork.class);
userService.insertUser(params);
return new ResponseResult();
}
private Object getEntity(String name, Class clazz){
return JSONObject.parseObject(JSONObject.toJSONString(name), clazz);
}](https://img-blog.csdnimg.cn/20190701150757328.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FkZWVwdHJhcA==,size_16,color_FFFFFF,t_70)
@RequestBody 이후의 매개 변수는 맵일 수도 있고 실체 클래스일 수도 있습니다. 실체 클래스일 경우 이 클래스에 여러 개의 대상과 get을 명시해야 합니다. set 방법은 맵일 수도 있고 중간 대상이 필요하지 않아도 여러 대상에 데이터를 실례화할 수 있습니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Spring Framework] 10강 - RESTful API그런데 RESTful API를 쓴 경우는 URL에 행위가 안써져 있고 단순히 특정 데이터 명시가 필요할 경우만 데이터를 명시하고 끝이 납니다. 특정 언어에 종속되지 않으며, 대부분의 프로그래밍 언어에서 JSON 포맷...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.