1.@PathVariable用法
@GetMapping("spu/updatesaleable/{id}")//注意此处注解加上参数
public ResponseEntity<Void> updatezhuangtai(@PathVariable("id")Long id){//此处注解也需要加上参数
this.goodsService.updatesaleable(id);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
所对应的前端
updatesaleable(id){
this.$message.confirm("确认要改变状态吗?")
.then(() => {
this.$http.get("/item/spu/updatesaleable/" + id)//注意传参方法为("/**/**/"+参数)
.then(() => {
this.$message.success("修改成功");
this.getDataFromServer();
})
})
}
2.@RequestParam用法
@RequestParam注解主要有哪些参数:
value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;
defaultValue:默认值,表示如果请求中没有同名参数时的默认值
@GetMapping("page")//此处注解无参数
public ResponseEntity<PageResult<Brand>> queryBrandsByPage(
@RequestParam(value = "key", required = false)String key,
@RequestParam(value = "page", defaultValue = "1")Integer page,
@RequestParam(value = "rows", defaultValue = "5")Integer rows,
@RequestParam(value = "sortBy", required = false)String sortBy,
@RequestParam(value = "desc", required = false)Boolean desc
){
PageResult<Brand> pageResult = this.brandService.queryBrandsByPage(key, page, rows, sortBy, desc);
if (CollectionUtils.isEmpty(pageResult.getItems())){
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(pageResult);
}
所对应的前端
getDataFromServer() { // 从服务的加载数的方法。
// 发起请求
this.$http.get("/item/brand/page", {
params: {
key: this.search, // 搜索条件
page: this.pagination.page,// 当前页
rows: this.pagination.rowsPerPage,// 每页大小
sortBy: this.pagination.sortBy,// 排序字段
desc: this.pagination.descending// 是否降序
}
}).then(({data}) => { // 这里使用箭头函数
this.brands = data.items;
this.totalBrands = data.total;
// 完成赋值后,把加载状态赋值为false
this.loading = false;
})
}
@PostMapping//此处无参数
public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids")List<Long> cids){ //此处有(”参数“)
this.brandService.saveBrand(brand,cids);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
对应前端
addBrand() {
// 修改标记
this.isEdit = false;
// 控制弹窗可见:
this.show = true;
// 把oldBrand变为null
this.oldBrand = null;
}
3.@RequestBody用法
@PostMapping("add")//此处无参数
public void toAdd(@RequestBody Map<String,Map<String,Category>> map){ //不写(”参数“)
Map<String,Category> params=map.get("params");
Category node = params.get("node");
this.categoryService.add(node);
}
对应后端
methods: {
handleAdd(node) {
this.$http.post("/item/category/add",{
params:{
node:node
}
})
console.log("add .... ");
console.log(node);
}