서비스 측이 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 방법은 맵일 수도 있고 중간 대상이 필요하지 않아도 여러 대상에 데이터를 실례화할 수 있습니다.

좋은 웹페이지 즐겨찾기