jsonpropertyorder注解,SpringMvc-PropertyEditor接口和@InitBinder注解

 2023-10-15 阅读 30 评论 0

摘要:注:通過InitBinder方法注冊PropertyEditor,在@ResponseBody注解的方法是無效的 1.自己定義一個類,這個類實現PropertyEditor接口,其實就是實現這個接口中的setAsText和getValue 1 springmvc會調用setAsText方法將request中對應的值傳遞進去 2

注:通過InitBinder方法注冊PropertyEditor,在@ResponseBody注解的方法是無效的

1.自己定義一個類,這個類實現PropertyEditor接口,其實就是實現這個接口中的setAsText和getValue

1 springmvc會調用setAsText方法將request中對應的值傳遞進去
2 springmvc會調用getValue方法,將拿到的值賦值到controller中的form實體類中

下面實例顯示的是無論前臺傳遞什么參數,最終User的name都是"小剛"

public class MyStringPropertyEditor implements PropertyEditor{@Overridepublic Object getValue() {return "小剛";}@Overridepublic void setAsText(String text) throws IllegalArgumentException {System.out.println("request中傳遞過來的值:"+text);}//省略其他Override方法
}

controller

@Controller
@RequestMapping("initbinder")
public class Controller {@InitBinderpublic void aa(WebDataBinder binder) {MyStringPropertyEditor sdf=new MyStringPropertyEditor();//表示User121中如果是String類型,就用MyStringPropertyEditor屬性編輯器binder.registerCustomEditor(String.class, sdf);}@ResponseBody@RequestMapping("test")public String aa(User121 user) {//無論URL傳遞什么,此處都會打印"小剛"System.out.println("controller:"+user.getName());return "GG";}
}

jsonpropertyorder注解,URL中雖然寫的是周傳雄,但是進入到controller中,依然變成了小剛

http://localhost:8080/initbinder/test?name=周傳雄

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/137082.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息