spring boot 學習筆記(005)返回json對象

前面的response,返回的都是String對象。現在到處都在用json對象。如果可以直接把一個數據對象通過json格式返回給response多好啊。

還以爲要費好大勁去配置。到網上找了N久,怎麼就沒人介紹一下呢。


抱着死馬當活馬醫的心態,自己試試把函數返回值改成對象呢。

	@RequestMapping("/rundemo")  
    @ResponseBody  
    public UserInfo demo(){
		logger.debug("just test the log. (/rundemo)");
		UserInfo u = new UserInfo();
		u.setUserCode("h001");
		u.setUserName("u name");
		u.setDeptCode("ab001");
        return u;  
    }  

好吧。在瀏覽器裏訪問:

http://localhost:8090/rundemo


獲得:

{"userCode":"h001","userName":"u name","deptCode":"ab001"}

這也太簡單了吧。沒有任何配置,這就無縫對接了啊。怪不得找不到資料呢,原來,原來,就跟 1 + 1 = 2 一樣啊。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章