乐云网站建设,中国建设银行网站成都第七支行,有专门学做衣服网站有哪些,网络公司网站优化网站建设文章目录 前言一、RequestBody二、RequestEntity三、ResponseBody四、SpringMVC处理json五、SpringMVC处理ajax六、RestController注解七、ResponseEntity总结 前言
HttpMessageConverter#xff0c;报文信息转换器#xff0c;将请求报文转换为Java对象#xff0c;或将Java… 文章目录 前言一、RequestBody二、RequestEntity三、ResponseBody四、SpringMVC处理json五、SpringMVC处理ajax六、RestController注解七、ResponseEntity总结 前言
HttpMessageConverter报文信息转换器将请求报文转换为Java对象或将Java对象转换为响应报文HttpMessageConverter提供了两个注解和两个类型RequestBodyResponseBodyRequestEntityResponseEntity。 一、RequestBody
RequestBody可以获取请求体需要在控制器方法设置一个形参使用RequestBody进行标识当前请求的请求体就会为当前注解所标识的形参赋值。
form th:action{/testRequestBody} methodpost
用户名input typetext nameusernamebr
密码input typepassword namepasswordbr
input typesubmit
/formRequestMapping(/testRequestBody)
public String testRequestBody(RequestBody String requestBody){
System.out.println(requestBody:requestBody);
return success;
}输出结果 requestBody:usernameadminpassword123456
二、RequestEntity
RequestEntity封装请求报文的一种类型需要在控制器方法的形参中设置该类型的形参当前请求的请求报文就会赋值给该形参可以通过getHeaders()获取请求头信息通过getBody()获取请求体信息。
form th:action{/testRequestEntity} methodpost用户名input typetext nameusernamebr密码input typepassword namepasswordbrinput typesubmit value测试RequestEntity
/formRequestMapping(/testRequestEntity)
public String testRequestEntity(RequestEntityString requestEntity){
System.out.println(requestHeader:requestEntity.getHeaders());
System.out.println(requestBody:requestEntity.getBody());
return success;
}输出结果 requestHeader:[host:“localhost:8080”, connection:“keep-alive”, content-length:“27”, cache-control:“max-age0”, 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:usernameadminpassword123
三、ResponseBody
ResponseBody用于标识一个控制器方法可以将该方法的返回值直接作为响应报文的响应体响应到浏览器。
a th:href{/testResponseBody}通过ResponseBody对象响应浏览器数据/aRequestMapping(/testResponseBody)
ResponseBody
public String testResponseBody(){
return success;
}结果 浏览器页面显示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
groupIdcom.fasterxml.jackson.core/groupId
artifactIdjackson-databind/artifactId
version2.12.1/version
/dependency在SpringMVC的核心配置文件中开启mvc的注解驱动此时在HandlerAdaptor中会自动装配一个消息转换器MappingJackson2HttpMessageConverter可以将响应到浏览器的Java对象转换为Json格式的字符串.
pom.xml
mvc:annotation-driven /在处理器方法上使用ResponseBody注解进行标识 将Java对象直接作为控制器方法的返回值返回就会自动转换为Json格式的字符串
RequestMapping(/testResponseUser)
ResponseBody
public User testResponseUser(){
return new User(1001,admin,123456,23,男);
}a th:href{/testResponseUser}通过ResponseUser对象响应浏览器数据/a浏览器的页面中展示的结果 {“id”:1001,“username”:“admin”,“password”:“123456”,“age”:23,“sex”:“男”}
五、SpringMVC处理ajax
div idapp
a th:href{/testAjax} clicktestAjaxtestAjax/abr
/divscript typetext/javascript th:src{/static/js/vue.js}/script
script typetext/javascript th:src{/static/js/axios.min.js}/script
script typetext/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();
}
}
});
/scriptRequestMapping(/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用于控制器方法的返回值类型该控制器方法的返回值就是响应到浏览器的响应报文。 总结
以上就是HttpMessageConverter有关讲解。