• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

前后端分离 后端第接口的实现

武飞扬头像
快乐星球哇
帮助1

1、service层

(1)接口

  1.  
     
  2.  
    public interface CatalogService {
  3.  
    CommonResponse<List<Category>> getCategoryList();
  4.  
    }

(2)实现

  1.  
    @Service("catalogService")
  2.  
    public class CatalogServiceImpl implements CatalogService {
  3.  
     
  4.  
    @Autowired
  5.  
    private CategoryMapper categoryMapper;
  6.  
     
  7.  
    @Override
  8.  
    public CommonResponse<List<Category>> getCategoryList() {
  9.  
    List<Category> categoryList = categoryMapper.selectList(null);
  10.  
    if(categoryList.isEmpty()){
  11.  
    return CommonResponse.createForSuccessMessage("没有分类信息");
  12.  
    }
  13.  
    return CommonResponse.createForSuccess(categoryList);
  14.  
    }
  15.  
    }
学新通

2、controller层

  1.  
    @Controller
  2.  
    @RequestMapping("/catalog/")
  3.  
    public class CatalogController {
  4.  
     
  5.  
    @Autowired
  6.  
    private CatalogService catalogService;
  7.  
     
  8.  
    @GetMapping("categories")
  9.  
    @ResponseBody
  10.  
    public CommonResponse<List<Category>> getCategoryList(){
  11.  
    return catalogService.getCategoryList();
  12.  
    }
  13.  
    }

3、运行后到网页中查看,利用JSON格式化工具,可以看到,除了返回了status、查询的结果,还返回了msg和success,没有意义,所以说要进行修改学新通

 (因为JSON会去找响应类当中所有的public并且是非静态的方法,构造方法除外,把这些值都序列化成对象返回)

4、回到通用响应类中

(1)在方法上加上@JsonIgnore,使得数据不会被序列化后返回

  1.  
    @JsonIgnore
  2.  
    public boolean isSuccess(){
  3.  
    return this.status == ResponseCode.SUCCESS.getCode();
  4.  
    }

(2)在类上打上注解,使那些值为空的属性不会被返回

  1.  
    @Getter
  2.  
    //JSON序列化时,空的数据不会被包含进去
  3.  
    @JsonInclude(JsonInclude.Include.NON_NULL)
  4.  
    public class CommonResponse<T> implements Serializable

5、结果:

学新通

这样就算完成啦

再来一个样例:

1、controller

  1.  
    @GetMapping("categories/{id}")
  2.  
    @ResponseBody
  3.  
    public CommonResponse<Category> getCategory(@PathVariable("id") String categoryId){
  4.  
    return null;
  5.  
    }

2、service

(1)接口

CommonResponse<Category> getCategory(String categoryId);

(2)实现

  1.  
    @Override
  2.  
    public CommonResponse<Category> getCategory(String categoryId) {
  3.  
    Category category = categoryMapper.selectById(categoryId);
  4.  
    if(category == null){
  5.  
    return CommonResponse.createForSuccessMessage("没有该ID的Category");
  6.  
    }
  7.  
    return CommonResponse.createForSuccess(category);
  8.  
    }

3、再回到controller,修改返回值

  1.  
    @GetMapping("categories/{id}")
  2.  
    @ResponseBody
  3.  
    public CommonResponse<Category> getCategory(@PathVariable("id") String categoryId){
  4.  
    return catalogService.getCategory(categoryId);
  5.  
    }

4、运行后在Postman中查看

(1)数据库中有DOGS类学新通

(2)数据库中没有DOG类

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfkagf
系列文章
更多 icon
同类精品
更多 icon
继续加载