springMVC中HttpMessageConverter的具體使用
前言
HttpMessageConverter,報文信息轉(zhuǎn)換器,將請求報文轉(zhuǎn)換為Java對象,或?qū)ava對象轉(zhuǎn)換為響應(yīng)報文HttpMessageConverter提供了兩個注解和兩個類型:@RequestBody,@ResponseBody,RequestEntity,ResponseEntity。
一、@RequestBody
@RequestBody可以獲取請求體,需要在控制器方法設(shè)置一個形參,使用@RequestBody進行標識,當前請求的請求體就會為當前注解所標識的形參賦值。
<form th:action="@{/testRequestBody}" method="post"> 用戶名:<input type="text" name="username"><br> 密碼:<input type="password" name="password"><br> <input type="submit"> </form>
@RequestMapping("/testRequestBody") public String testRequestBody(@RequestBody String requestBody){ System.out.println("requestBody:"+requestBody); return "success"; }
輸出結(jié)果:
requestBody:username=admin&password=123456
二、RequestEntity
RequestEntity封裝請求報文的一種類型,需要在控制器方法的形參中設(shè)置該類型的形參,當前請求的請求報文就會賦值給該形參,可以通過getHeaders()獲取請求頭信息,通過getBody()獲取請求體信息。
<form th:action="@{/testRequestEntity}" method="post"> 用戶名:<input type="text" name="username"><br> 密碼:<input type="password" name="password"><br> <input type="submit" value="測試RequestEntity"> </form>
@RequestMapping("/testRequestEntity") public String testRequestEntity(RequestEntity<String> requestEntity){ System.out.println("requestHeader:"+requestEntity.getHeaders()); System.out.println("requestBody:"+requestEntity.getBody()); return "success"; }
輸出結(jié)果:
requestHeader:[host:“localhost:8080”, connection:“keep-alive”, content-length:“27”,
cache-control:“max-age=0”, sec-ch-ua:“” Not A;Brand";v=“99”, “Chromium”;v=“90”, “Google
Chrome”;v=“90"”, sec-ch-ua-mobile:“?0”, upgrade-insecure-requests:“1”, origin:“http://localhost:8
080”, user-agent:“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/90.0.4430.93 Safari/537.36”] requestBody:username=admin&password=123
三、@ResponseBody
@ResponseBody用于標識一個控制器方法,可以將該方法的返回值直接作為響應(yīng)報文的響應(yīng)體響應(yīng)到瀏覽器。
<a th:href="@{/testResponseBody}" rel="external nofollow" >通過ResponseBody對象響應(yīng)瀏覽器數(shù)據(jù)</a>
@RequestMapping("/testResponseBody") @ResponseBody public String testResponseBody(){ return "success"; }
結(jié)果:
瀏覽器頁面顯示success
四、SpringMVC處理json
User類:
package com.dragon.mvc.bean; public class User { private Integer id; private String username; private String password; private Integer age; private String sex; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public User(Integer id, String username, String password, Integer age, String sex) { this.id = id; this.username = username; this.password = password; this.age = age; this.sex = sex; } public User() { } }
@ResponseBody處理json的步驟:
- 導入jackson的依賴
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.1</version> </dependency>
- 在SpringMVC的核心配置文件中開啟mvc的注解驅(qū)動,此時在HandlerAdaptor中會自動裝配一個消息轉(zhuǎn)換器:MappingJackson2HttpMessageConverter,可以將響應(yīng)到瀏覽器的Java對象轉(zhuǎn)換為Json格式的字符串.
pom.xml======================= <mvc:annotation-driven />
在處理器方法上使用@ResponseBody注解進行標識
將Java對象直接作為控制器方法的返回值返回,就會自動轉(zhuǎn)換為Json格式的字符串
@RequestMapping("/testResponseUser") @ResponseBody public User testResponseUser(){ return new User(1001,"admin","123456",23,"男"); }
<a th:href="@{/testResponseUser}" rel="external nofollow" >通過ResponseUser對象響應(yīng)瀏覽器數(shù)據(jù)</a>
瀏覽器的頁面中展示的結(jié)果:
{“id”:1001,“username”:“admin”,“password”:“123456”,“age”:23,“sex”:“男”}
五、SpringMVC處理ajax
<div id="app"> <a th:href="@{/testAjax}" rel="external nofollow" @click="testAjax">testAjax</a><br> </div>
<script type="text/javascript" th:src="@{/static/js/vue.js}"></script> <script type="text/javascript" th:src="@{/static/js/axios.min.js}"></script> <script type="text/javascript"> var vue = new Vue({ el:"#app", methods:{ testAjax:function (event) { axios({ method:"post", url:event.target.href, params:{ username:"admin", password:"123456" } }).then(function (response) { alert(response.data); }); event.preventDefault(); } } }); </script>
@RequestMapping("/testAjax") @ResponseBody public String testAjax(String username, String password){ System.out.println("username:"+username+",password:"+password); return "hello,ajax"; }
六、@RestController注解
@RestController注解是springMVC提供的一個復合注解,標識在控制器的類上,就相當于為類添加了@Controller注解,并且為其中的每個方法添加了@ResponseBody注解。
七、ResponseEntity
ResponseEntity用于控制器方法的返回值類型,該控制器方法的返回值就是響應(yīng)到瀏覽器的響應(yīng)報文。
總結(jié)
到此這篇關(guān)于springMVC中HttpMessageConverter的具體使用的文章就介紹到這了,更多相關(guān)springMVC HttpMessageConverter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringMVC配置javaConfig及StringHttpMessageConverter示例
- SpringMVC HttpMessageConverter消息轉(zhuǎn)換器
- SpringMVC HttpMessageConverter報文信息轉(zhuǎn)換器
- SpringMVC?HttpMessageConverter報文信息轉(zhuǎn)換器
- springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的裝配講解
- SpringMVC源碼解析之消息轉(zhuǎn)換器HttpMessageConverter
- SpringMVC 中HttpMessageConverter簡介和Http請求415 的問題
相關(guān)文章
Mybatis-Plus @TableField自動填充時間為null的問題解決
本文主要介紹了Mybatis-Plus @TableField自動填充時間為null的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01Java編程通過匹配合并數(shù)據(jù)實例解析(數(shù)據(jù)預(yù)處理)
這篇文章主要介紹了Java編程通過匹配合并數(shù)據(jù)實例解析(數(shù)據(jù)預(yù)處理),分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01java底層AQS實現(xiàn)類ReentrantLock鎖的構(gòu)成及源碼解析
本章我們就要來學習一下第一個?AQS?的實現(xiàn)類:ReentrantLock,看看其底層是如何組合?AQS?,實現(xiàn)了自己的那些功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03Eclipse中maven異常Updating Maven Project的統(tǒng)一解決方案
今天小編就為大家分享一篇關(guān)于Eclipse中maven異常Updating Maven Project的統(tǒng)一解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12超詳細講解SpringCloud?Commons公共抽象的用法
這篇文章主要介紹了超詳細講解SpringCloud?Commons公共抽象的用法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04