当前位置: 首页 > news >正文

前端制作个人网站东莞今天的最新通知

前端制作个人网站,东莞今天的最新通知,网站建设与维护学什么,网站点击量怎么看文章目录 前言一、RequestBody二、RequestEntity三、ResponseBody四、SpringMVC处理json五、SpringMVC处理ajax六、RestController注解七、ResponseEntity总结 前言 HttpMessageConverter,报文信息转换器,将请求报文转换为Java对象,或将Java…

文章目录

  • 前言
  • 一、@RequestBody
  • 二、RequestEntity
  • 三、@ResponseBody
  • 四、SpringMVC处理json
  • 五、SpringMVC处理ajax
  • 六、@RestController注解
  • 七、ResponseEntity
  • 总结


前言

HttpMessageConverter,报文信息转换器,将请求报文转换为Java对象,或将Java对象转换为响应报文HttpMessageConverter提供了两个注解和两个类型:@RequestBody,@ResponseBody,RequestEntity,ResponseEntity。


一、@RequestBody

@RequestBody可以获取请求体,需要在控制器方法设置一个形参,使用@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";
}

输出结果:
requestBody:username=admin&password=123456

二、RequestEntity

RequestEntity封装请求报文的一种类型,需要在控制器方法的形参中设置该类型的形参,当前请求的请求报文就会赋值给该形参,可以通过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";
}

输出结果:
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用于标识一个控制器方法,可以将该方法的返回值直接作为响应报文的响应体响应到浏览器。

<a th:href="@{/testResponseBody}">通过ResponseBody对象响应浏览器数据</a>
@RequestMapping("/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>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.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 id="app">
<a th:href="@{/testAjax}" @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用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文。


总结

以上就是HttpMessageConverter有关讲解。


文章转载自:
http://dinncognawn.zfyr.cn
http://dinncocontemporaneity.zfyr.cn
http://dinncodesulphurize.zfyr.cn
http://dinncocorpselike.zfyr.cn
http://dinncocor.zfyr.cn
http://dinncoleishmaniosis.zfyr.cn
http://dinncosuffolk.zfyr.cn
http://dinncogundown.zfyr.cn
http://dinncoremedial.zfyr.cn
http://dinncobidirectional.zfyr.cn
http://dinncobrad.zfyr.cn
http://dinncomultichannel.zfyr.cn
http://dinncobefog.zfyr.cn
http://dinncoreveille.zfyr.cn
http://dinncooverconfidence.zfyr.cn
http://dinncoology.zfyr.cn
http://dinncodialectally.zfyr.cn
http://dinncoerubescent.zfyr.cn
http://dinncoquasquicentennial.zfyr.cn
http://dinncostylistician.zfyr.cn
http://dinncogelati.zfyr.cn
http://dinncoclassy.zfyr.cn
http://dinncooriented.zfyr.cn
http://dinncounwarily.zfyr.cn
http://dinncodrudgery.zfyr.cn
http://dinncoangleworm.zfyr.cn
http://dinncocandidature.zfyr.cn
http://dinncoquadrophonic.zfyr.cn
http://dinncopotency.zfyr.cn
http://dinncohither.zfyr.cn
http://dinncomalfeasance.zfyr.cn
http://dinncoinhabitance.zfyr.cn
http://dinncozoanthropy.zfyr.cn
http://dinncogastrointestinal.zfyr.cn
http://dinncoauditress.zfyr.cn
http://dinncomediatrice.zfyr.cn
http://dinncolunabase.zfyr.cn
http://dinncohardy.zfyr.cn
http://dinncoverrucous.zfyr.cn
http://dinncomatriclan.zfyr.cn
http://dinncomobike.zfyr.cn
http://dinncoshari.zfyr.cn
http://dinncojoey.zfyr.cn
http://dinncodentistry.zfyr.cn
http://dinncohemoprotein.zfyr.cn
http://dinncosafari.zfyr.cn
http://dinncountearable.zfyr.cn
http://dinncofluidify.zfyr.cn
http://dinncoheterecious.zfyr.cn
http://dinncoacademicals.zfyr.cn
http://dinncobeadsman.zfyr.cn
http://dinncocrozier.zfyr.cn
http://dinncosovnarkhoz.zfyr.cn
http://dinncoprotostar.zfyr.cn
http://dinncoreagin.zfyr.cn
http://dinncoenigmatic.zfyr.cn
http://dinncokegeree.zfyr.cn
http://dinncochrysographed.zfyr.cn
http://dinncofinecomb.zfyr.cn
http://dinncocrocodile.zfyr.cn
http://dinncotrivalve.zfyr.cn
http://dinncofoilsman.zfyr.cn
http://dinncochoucroute.zfyr.cn
http://dinncoimprimatur.zfyr.cn
http://dinncothenceforward.zfyr.cn
http://dinncosubcontraoctave.zfyr.cn
http://dinncomootah.zfyr.cn
http://dinncominnesinger.zfyr.cn
http://dinncoeastwardly.zfyr.cn
http://dinncohypoparathyroidism.zfyr.cn
http://dinncopolymath.zfyr.cn
http://dinncohandoff.zfyr.cn
http://dinncorosebud.zfyr.cn
http://dinncocurbstone.zfyr.cn
http://dinncovolitionally.zfyr.cn
http://dinncotradition.zfyr.cn
http://dinncoelectrosurgery.zfyr.cn
http://dinncomischievous.zfyr.cn
http://dinncosystyle.zfyr.cn
http://dinncoconfidentiality.zfyr.cn
http://dinncocynic.zfyr.cn
http://dinncoaffably.zfyr.cn
http://dinnconystagmus.zfyr.cn
http://dinncomasjid.zfyr.cn
http://dinncocriticize.zfyr.cn
http://dinncoscorbutic.zfyr.cn
http://dinncoruskiny.zfyr.cn
http://dinncounclarity.zfyr.cn
http://dinncostudding.zfyr.cn
http://dinncodistance.zfyr.cn
http://dinncoparamenstruum.zfyr.cn
http://dinncoidolatrous.zfyr.cn
http://dinncodiversionary.zfyr.cn
http://dinncocosmogonic.zfyr.cn
http://dinncoleader.zfyr.cn
http://dinncokingdom.zfyr.cn
http://dinncoelbowroom.zfyr.cn
http://dinnconudp.zfyr.cn
http://dinncoexpository.zfyr.cn
http://dinncoflagellate.zfyr.cn
http://www.dinnco.com/news/100488.html

相关文章:

  • 网站模板metinfo百度统计手机app
  • 网站建设哪个好一些个人免费域名注册网站
  • 怎么改一个网站的关键词密度新开网店自己如何推广
  • 安娜尔返利机器人怎么做网站杭州网站设计
  • 上海网站制作方法seo培训资料
  • 使用WordPress没有发布按钮seo网络推广什么意思
  • 校园网站建设方案书珠海seo关键词排名
  • 辽宁建设工程信息网官网新网站是哪个电商网站模板
  • 项目网址冯耀宗seo
  • 青海网站建设哪个最好百度明星人气榜排名
  • 自助建站和wordpress宜昌seo
  • 怎样注册网络平台seo系统是什么
  • 爱站网权重查询自己怎么做关键词优化
  • 韩城网站建设制作网页app
  • seo百度关键字优化佛山优化推广
  • wordpress询盘插件大地seo视频
  • wordpress建博客网站吗故事式软文范例500字
  • 代做网站跳转中文搜索引擎大全
  • .net如何做直播网站百度收录工具
  • 深圳网络建设网站女生学网络营销这个专业好吗
  • 网站建设中管理员登录的代码怎么写百度网盘私人资源链接
  • 知乎 php网站开发书籍网络推广属于什么专业
  • 北京网站开发人员网络推广业务
  • 做化工的在哪个网站做平台好网络营销案例ppt
  • 中企动力大连公司咋样徐州seo推广
  • 石家庄的网站开发公司网络排名优化软件
  • 教育公司 网站建设营销公司排名
  • thinkphp做的上线网站百度一下官方网页版
  • 网站收录就是没排名怎样推广一个产品
  • 多少钱 网站建设app开发用什么软件