这可以通过自定义编辑器完成,该编辑器将JSON转换为UserProfile对象:
public class UserProfileEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { ObjectMapper mapper = new ObjectMapper(); UserProfile value = null; try { value = new UserProfile(); JsonNode root = mapper.readTree(text); value.setEmail(root.path("email").asText()); } catch (IOException e) { // handle error } setValue(value); }}这是为了在控制器类中注册编辑器:
@InitBinderpublic void initBinder(WebDataBinder binder) { binder.registerCustomEditor(UserProfile.class, new UserProfileEditor());}这是使用编辑器解组JSONP参数的方法:
@RequestMapping(value = "/jsonp", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})@ResponseBodySessionInfo register(@RequestParam("profileJson") UserProfile profileJson){ ...}


