restful

常见的HTTP请求格式   

    PUT、GET

    但其实还有两种请求格式 :DELETE、POST

未使用restful

http://127.0.0.1/user/query/1 GET 根据用户id查询用户数据
http://127.0.0.1/user/save POST 新增用户
http://127.0.0.1/user/update POST 修改用户信息
http://127.0.0.1/user/delete/1 GET/POST 删除用户信息


使用restful
http://127.0.0.1/user/1  GET根据用户id查询用户数据
http://127.0.0.1/user      POST 新增用户
http://127.0.0.1/user     PUT 修改用户信息
http://127.0.0.1/user   DELETE 删除用户信息
 
 
      (其中@RequestHeader)表示在请求头中 对应Headers,(@RequestParam)对应Params  (@RequestBody)对应Body
  
@GetMapping(path = "items", name = "查询字典项")
public List<DictItem> queryItem(@RequestHeader(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestHeader(name = "pageSize", defaultValue = "15") Integer pageSize,
@RequestParam(name = "dictId") String dictId,
@RequestParam(name = "codeOrName", required = false) String codeOrName,
@RequestParam(name = "sort", defaultValue = "seq", required = false) String sort,
@RequestParam(name = "asc", defaultValue = "true", required = false) boolean asc) {

log.info("查询字典项列表");

EntityWrapper<DictItem> ew = new EntityWrapper<>();

ew.eq("c_dict_id",dictId);

if (!StringUtils.isEmpty(codeOrName)) {
ew.andNew();
ew.like("c_code", codeOrName);
ew.or();
ew.like("c_name", codeOrName);
}
ew.orderBy(cols.get(sort), asc);

Page<DictItem> page = new Page<>(pageNo, pageSize);
page = itemService.selectPage(page, ew);

log.info("总数:{}", page.getTotal());
response.setIntHeader(CommonConst.RECORD_TOTAL, (int) page.getTotal());
return page.getRecords();

}
 
@PostMapping(name = "添加部门")
public String add(@RequestBody Department dept){
deptService.insert(dept);
return dept.getId();
}

@PutMapping(path = "{id}", name = "更新字典")
public void update(@PathVariable String id, @RequestBody @Valid Dict dict) {

Dict old = dictService.selectById(id);
log.info("更新字典【{}】", old.getName());
dictService.updateById(dict);
// 清缓存
DictCache.clearDict(old.getCode());
}



@DeleteMapping(path = "items/{itemId}", name = "删除字典项")
public void deleteItem(@PathVariable(name = "itemId") String itemId) {

itemService.deleteById(itemId);
}



以下为restful返回的数据格式,分为 code,msg,data  data表示返回的数据

posted @ 2019-12-24 15:48  wangt1  阅读(646)  评论(0编辑  收藏  举报