理解Restful
REST是Representational State Transfer的缩写,翻译是”表现层状态转化”。 可以 总结为一句话:REST是所有Web应用都应该遵守的架构设计指导原则。
7个HTTP方法:GET/POST/PUT/DELETE/PATCH/HEAD/OPTIONS。
测试工具Postman
发送HTTP请求的工具。
公共子模块
1.返回结果实体类Result
public class Result{
private boolean flag; //是否成功
private Integer code; //返回码
private String message; //返回信息
private Object data; //返回数据
//成功并且携带参数的构造器
public Result(boolean flag,Integer code,String message,Object data){
super();
this.flag = flag;
this,code = code;
this.message = message;
this.data = data;
}
//失败的构造器
public Result(boolean flag,Integer code,String message){
super();
this.flag = flag;
this.code = code;
this.message = message;
}
//setter/getter...
}
2.返回分页结果的PageResult
public class PageResult<T>{
private Long total;
private List<T> rows;
public PageResult(Long total,List<T> rows){
super();
this.total = total;
this.rows = rows;
}
//setter/getter
}
3.分布式ID生成器
@SpringBootApplication
public class BaseApplication{
public static void main(String[] args){
SpringBootApplication.run(BaseApplication.class);
}
@Bean
public IdWorker idWorker(){
return new IdWorker(1,1);
}
}
CRUD的实现
1.实体类
@Entity
@Table(name = "tb_label")
public class Label{
@Id
private String id; //
private String labelName; //标签名称
private String state; //状态
private Long count; //使用数量
//setters/getters
}
2.Jpa数据访问接口
public interface LabelDao extends JpaRespository<Label,String>,JpaSpecificationExecutor<Label>{
}
3.业务逻辑类
@Service
public class LabelService{
@Autowired
private LabelDao labelDao;
@Autowired
private IdWorker idWorker;
public List<Label> findAll(){
return labelDao.findAll();
}
public Label findById(String id){
return labelDao.findById(id).get();
}
public void add(Label label){
label.setId(idWorker.nextId() + "");
labelDao.save(label);
}
public void update(Label label){
labelDao.save(label);
}
public void deleteById(String id){
labelDao.deleteById(id);
}
}
4.控制器类
@RestController
@RequestMapping("/label")
public class LabelController{
@Autowired
private LabelService labelService;
/*查询全部列表
* @return
*/
@RequestMapping(method = RequestMethod.GET)
public Result<List> findAll(){
return new Result<>(true,StatusCode.OK,"查询成功",labelService.findAll());
}
/**
* 根据ID查询标签
* @param id
* @return
*/
@RequestMapping(value = /{id},method = RequestMethod.GET)
public Result<Label> findById(@PathVariable String id){
return new Result<>(true,StatusCode.OK,"查询成功",labelService.findById(id));
}
/**
* @param id
* @return
**/
@RequestMapping(method = RequestMethod.POST)
public Result add(@RequestBody Label label){
labelService.add(label);
return new Result(true,StatusCode.OK,"增加成功");
}
/**
* 修改标签
* @param label
* @return
*/
@RequestMapping(value="/{id}" ,method = RequestMethod.PUT)
public Result update( @RequestBody Label label,@PathVariable String id){
label.setId(id);
labelService.update(label);
return new Result(true,StatusCode.OK,"修改成功");
}
/**
* 删除标签
* @param id
* @return
*/
@RequestMapping(value="/{id}" ,method = RequestMethod.DELETE)
public Result deleteById(@PathVariable String id){
labelService.deleteById(id);
return new Result(true,StatusCode.OK,"删除成功");
}
}
5.接口测试
image.png