ObjectMapper 中的兩個方法 中writeValue和writeValueAsString的區別


https://blog.csdn.net/qq_43061290/article/details/104577391

```
//把保存數據序列化為json數據
ObjectMapper mapper=new ObjectMapper();
String josn = mapper.writeValueAsString(info);
//設置content-Type
response.setContentType("application/json;charset=utf-8");
//把json寫到前台頁面
response.getWriter().write(josn);
```
直接寫給前端不做任何處理

```
ObjectMapper mapper = new ObjectMapper();

response.setContentType("application/json;charset=utf-8");
mapper.writeValue(response.getOutputStream(),info);

```
寫給前端之前先把數據封裝到session中,一般用於登陸后在前端顯示用戶名


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM