
致力于最高效的Java学习
关注npm install axios
然后创建 Vue 工程,手动导入 axios 组件,命令如下所示。vue add axios
Vue 环境搭建好之后,创建 Spring Boot 工程,之后就可以分别完成前后端代码的开发。1、GET 请求 + 普遍变量传参axios 异步 GET 请求的方法为 axios.get(url,params).then()url:请求的 URL。params:参数,格式为 {params:{name:value,name:value}}then():请求成功的回调函数。Vue 代码如下所示。<template> <div> <button type="button" @click="getData()">getDatabutton><br/> div>template><script> export default { methods: { getData(){ const _this = this axios.get('http://localhost:8181/getData',{params:{id:1,name:'张三'}}).then(function (resp) { console.log(resp.data) }) } } }script>
Spring Boot 代码如下所示。@RestControllerpublic class DataHandler { @GetMapping("/getData") public String getData(Integer id,String name){ System.out.println(id); System.out.println(name); return id+name; }}
分别启动 Vue 和 Spring Boot,点击 getData按钮,结果如下图所示。@Configurationpublic class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
再次启动 Spring Boot,点击 getData按钮,结果如下图所示。<template> <div> <button type="button" @click="getJSON()">getJSONbutton><br/> div>template><script> export default { methods: { getJSON(){ const _this = this var user = { id:1, name:'张三' } axios.get('http://localhost:8181/getJSON',{params:user}).then(function (resp) { console.log(resp.data) }) } } }script>
Spring Boot 代码如下所示。@Datapublic class User { private Integer id; private String name;}
@GetMapping("/getJSON")public User getJSON(User user){ System.out.println(user); return user;}
分别启动 Vue 和 Spring Boot,点击 getJSON按钮,结果如下图所示。<template> <div> <button type="button" @click="postData()">postDatabutton><br/> div>template><script> export default { methods: { postData(){ const _this = this var params = new URLSearchParams() params.append('id', '1') params.append('name', '张三') axios.post('http://localhost:8181/postData',params).then(function (resp) { console.log(resp.data) }) } } }script>
Spring Boot 代码如下所示。@PostMapping("/postData")public User postData(Integer id,String name){ System.out.println(id); System.out.println(name); return new User(id,name);}
分别启动 Vue 和 Spring Boot,点击 postData按钮,结果如下图所示。<template> <div> <button type="button" @click="postJSON()">postJSONbutton><br/> div>template><script> export default { methods: { postJSON(){ const _this = this var params = new URLSearchParams() params.append('id', '1') params.append('name', '张三') axios.post('http://localhost:8181/postJSON',params).then(function (resp) { console.log(resp.data) }) } } }script>
Spring Boot 代码如下所示。@PostMapping("/postJSON")public User postJSON(User user){ System.out.println(user); return new User(1,"张三");}
分别启动 Vue 和 Spring Boot,点击 postJSON按钮,结果如下图所示。<template> <div> <button type="button" @click="restGetData()">restGetDatabutton><br/> div>template><script> export default { methods: { restGetData(){ const _this = this axios.get('http://localhost:8181/restGetData/1').then(function (resp) { console.log(resp.data) }) } } }script>
Spring Boot 代码如下所示。@GetMapping("/restGetData/{id}")public User restGetData(@PathVariable("id") Integer id){ System.out.println(id); return new User(1,"张三");}
分别启动 Vue 和 Spring Boot,点击 restGetData按钮,结果如下图所示。<template> <div> <button type="button" @click="restGetJSON()">restGetJSONbutton><br/> div>template><script> export default { methods: { restGetJSON(){ const _this = this axios.get('http://localhost:8181/restGetJSON',{params:{id:1,name:'张三'}}).then(function (resp) { console.log(resp.data) }) } } }script>
Spring Boot 代码如下所示。@GetMapping("/restGetJSON")public User restGetJSON(User user){ System.out.println(user); return new User(1,"张三");}
分别启动 Vue 和 Spring Boot,点击 restGetJSON按钮,结果如下图所示。推荐阅读
1、快速上手Spring Boot+Vue前后端分离
2、Vue+Element UI搭建后台管理系统界面
3、一文搞懂前后端分离
4、徒手撸一个Spring MVC框架