Spring MVC控制器中的JSON参数

Spring MVC控制器中的JSON参数,第1张

Spring MVC控制器中的JSON参数

这可以通过自定义编辑器完成,该编辑器将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){  ...}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4980846.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存