Restful编码风格的Spring代码的书写
1概念:
“ 动作+路径 ” 来代替不同的传参和底层servlet的这种资源的访问形式叫做rest, 用这种形式访问叫做restful l代码风格
1.2使用:
Users 带s表示模块,表示此类资源而非单个资源。 数字代表传入资源 post 增 delete 删 put 改 get 查
1.2.1增删改查:
package com.tang.testspring;
import com.tang.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SpringMVCRestful {
//增加数据
@RequestMapping(value = "/users",method = RequestMethod.POST)
@ResponseBody
public String sava(){
System.out.println("save data to mysql");
return "{'data','mysql'}";
}
//删除数据
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
System.out.println("delete data to mysql from id = " + id);
return "{'data','delete'}";
}
//修改数据
@RequestMapping(value = "/users",method = RequestMethod.PUT)
@ResponseBody
public String update(User user){
System.out.println(user);
return "{'data','update'}";
}
//查询数据
@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
@ResponseBody
public String get(@PathVariable Integer id){
System.out.println("get data from mysql");
return "{'data','get'}";
}
}
1.3简化SpringMVC的Restful接口开发:
其实就是简化了三个东西,
一个是 controller 和 responsebody合成一个注解叫做 restcontroller,
二个在就是直接把requestmapping提出来到类的上面,
三个把方法简化成 postmapping , deletemapping , putmapping, getmapping,方法里面记得加上id
直接上代码:
package com.tang.testspring;
import com.tang.pojo.User;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class SpringMVCRestful {
@PostMapping
public String save(){
System.out.println("save data");
return "{'data','save'}";
}
@DeleteMapping("/{id}")
public String delete(@PathVariable String id){
System.out.println("delete data");
return "{'data','delete'}";
}
@PutMapping
public String update(User usr){
System.out.println("save data");
return "{'data','update'}";
}
@GetMapping("/{id}")
public String get(@PathVariable String id){
System.out.println("get data");
return "{'data','get'}";
}
}
1.4用SpringMVC的Restful开发案例:
1.4.1配置前端界面以及放通前端界面的显示:
package com.tang.testspring;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class SpringPageConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
1.4.2调通SpringMVC和访问url
前端代码:
<!DOCTYPE html>
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<title>SpringMVC案例</title>
<!-- 引入样式 -->
<link rel="stylesheet" href="../plugins/elementui/index.css">
<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
<div class="content-header">
<h1>图书管理</h1>
</div>
<div class="app-container">
<div class="box">
<div class="filter-container">
<el-input placeholder="图书名称" style="width: 200px;" class="filter-item"></el-input>
<el-button class="dalfBut">查询</el-button>
<el-button type="primary" class="butT" @click="openSave()">新建</el-button>
</div>
<el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
<el-table-column type="index" align="center" label="序号"></el-table-column>
<el-table-column prop="type" label="图书类别" align="center"></el-table-column>
<el-table-column prop="name" label="图书名称" align="center"></el-table-column>
<el-table-column prop="description" label="描述" align="center"></el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-button type="primary" size="mini">编辑</el-button>
<el-button size="mini" type="danger">删除</el-button>
</template>
</el-table-column>
</el-table>
<div class="pagination-container">
<el-pagination class="pagiantion" @current-change="handleCurrentChange"
:current-page="pagination.currentPage" :page-size="pagination.pageSize"
layout="total, prev, pager, next, jumper" :total="pagination.total">
</el-pagination>
</div>
<!-- 新增标签弹层 -->
<div class="add-form">
<el-dialog title="新增图书" :visible.sync="dialogFormVisible">
<el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="图书类别" prop="type">
<el-input v-model="formData.type" />
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="图书名称" prop="name">
<el-input v-model="formData.name" />
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="描述">
<el-input v-model="formData.description" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取消</el-button>
<el-button type="primary" @click="saveBook()">确定</el-button>
</div>
</el-dialog>
</div>
</div>
</div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
var vue = new Vue({
el: '#app',
data: {
dataList: [],//当前页要展示的分页列表数据
formData: {},//表单数据
dialogFormVisible: false,//增加表单是否可见
dialogFormVisible4Edit: false,//编辑表单是否可见
pagination: {},//分页模型数据,暂时弃用
},
//钩子函数,VUE对象初始化完成后自动执行
created() {
this.getAll();
},
methods: {
// 重置表单
resetForm() {
//清空输入框
this.formData = {};
},
// 弹出添加窗口
openSave() {
this.dialogFormVisible = true;
this.resetForm();
},
//添加
saveBook() {
axios.post("/TestSpringMVC/books", this.formData).then((res) => {
});
},
//主页列表查询
getAll() {
axios.get("/TestSpringMVC/books").then((res) => {
this.dataList = res.data;
});
},
}
})
</script>
</html>
后端BookController代码:
package com.tang.controller;
import com.tang.pojo.Book;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
@PostMapping
public String sava(@RequestBody Book book){
System.out.println("this is the new book " + book);
return "{'info','saveName'}";
}
@GetMapping
public List<Book> getAll(){
System.out.println("this is a get all method");
List<Book> books = new ArrayList<>();
Book booka = new Book();
booka.setDescription("a little yellow");
booka.setId("a");
booka.setName("yellowbook");
booka.setType("newbook");
Book bookb = new Book();
bookb.setType("oldbook");
bookb.setId("b");
bookb.setName("bulebook");
bookb.setDescription("a little blue");
books.add(booka);
books.add(bookb);
return books;
}
}