• 浅谈rest風格的接口开发


    简单描述:因为前后端分离,开发完模块之后,接到team leader的指令,我这个渣渣javaer需要给前端人员返回一个接口,具体内容是课程列表json和分类列表json。emmmm,第一次写接口,心理是有点啪啪啪的,手误,怕怕的,完全不知道应该怎么写。不过,程序员从来都不会说做不到,能做到的是想方设法的去搞定他。最终,还是把它搞出来了,哈哈哈哈,experience有增长了一点点。

    过程:

    创建实体类,并且使用@Table和数据库表相对应,@Getter@Setter都是lombok包下的 @Id@Table@Column是javax.persistence包下的

    //实体类Course @Column中的是表字段 要注意的是必须和表完全对应,表里有多少字段,类就有多少属性 
    @Getter
    @Setter
    @Table(name = "table_course")
    public class Course {
    @Id
    @Column(name = "course_id")
    private String courseId;
    @Column(name = "course_name")
    private String courseName;
    @Column(name = "course_code")
    private String courseCode;
    @Column(name = "course_type")
    private String courseType;
    @Column(name = "is_del")
    private String isDel;//未删除 1 已删除 0
    } 

    //实体类Dictionary
    import lombok.Getter;
    import lombok.Setter;

    import javax.persistence.Column;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Getter
    @Setter
    @Table(name = "table_dict")
    public class Dictionary {
    @Id
    @Column(name = "dict_id")
    private String dictId;
    @Column(name = "dict_name")
    private String dictName;
    @Column(name = "dict_code")
    private String dictCode;
    @Column(name = "sort_no")
    private String sortNo;
    @Column(name = "parent_id")
    private String parentId;
    @Column(name = "description")
    private String description;
    } 

     Controller层

    //controller层 
    import xxx.xx.xxxxx.xxxx.xxxxx.ResultDto;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    import java.util.List;
    import java.util.Map;
    @Api(value = "CourseController",tags = "课程相关查询")
    @RestController
    @RequestMapping("/course")
    public class CourseController extends BaseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "获取课程列表" notes= "获取课程列表")
    @RequestMapping(value = "/queryList", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public ResultDto queryList(){
    List<CourseVo> list = null;
    try {
    list = courseService.queryList();
    if(list != null){
    if(list.size()<1){
    return ResultDto.success("返回结果无内容");
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    return ResultDto.error();
    }
    return ResultDto.success(list);
    }

    @ApiOperation(value = "获取课程分类列表",notes = "根据课程编码查询")
    @RequestMapping(value = "/getCourseType",method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public ResultDto getCourseType(String code){
    List<Map<String,String>> list = null;
    try {
    if (!"".equals(code) && code != null) {
    list = courseService.getCourseType(code);
    if(list != null){
    if(list.size()<1){
    return ResultDto.success("返回结果无内容");
    }
    }
    }else{
    return ResultDto.error("参数错误");
    }
    } catch (Exception e) {
    e.printStackTrace();
    return ResultDto.error();
    }
    return ResultDto.success(list);
    }
    } 

     Service层

    //service层
    import org.springframework.beans.BeanUtils;
    import org.springframework.stereotype.Service;
    import tk.mybatis.mapper.entity.Example;

    import javax.annotation.Resource;

    @Service
    public class CourseService {
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private DictionaryMapper dictionaryMapper;

    public List<CourseVo> queryList()throws Exception{
    Example example = new Example(Course.class);
    example.createCriteria().andEqualTo("isDel",1);
    List<Course> list = courseMapper.selectByExample(example);
    List<CourseVo> reList = new ArrayList<>();
    if (list !=null){
    for(Course obj:list){
    CourseVo course = new CourseVo();
    BeanUtils.copyProperties(obj,course);
    reList.add(course);
    }
    }
    return reList;
    }

    public List<Map<String,String>> getCourseType(String code) throws Exception{
    Dictionary obj = new Dictionary();
    obj.setDictCode(code);
    Dictionary dict = dictionaryMapper.selectOne(obj);
    Example example = new Example(Dictionary.class);
    example.createCriteria().andEqualTo("parentId",dict.getDictId());
    List<Dictionary> list = DictionaryMapper.selectByExample(example);
    List<Map<String,String>> reList = new ArrayList<>();
    if(list != null){
    for(Dictionary test:list){
    Map<String,String> map = new HashMap<>();
    map.put(test.getDictName(),test.getDictCode());
    reList.add(map);
    }
    }
    return reList;
    }
    } 

     对应的Mapper接口

    //CourseMapper接口
    import xxx.xx.xxxxxx.xxxx.util.MyMapper;

    public interface CourseMapper extends MyMapper<Course> {
    } 

    //DictionaryMapper接口
    import xxx.xx.xxxxxx.xxxx.util.MyMapper;

    public interface DictionaryMapper extends MyMapper<Dictionary>{
    }

     MyMapper<T>

    //MyMapper接口
    /*
    * The MIT License (MIT)
    *
    * Copyright (c) 2014-2016 abel533@gmail.com
    *
    * Permission is hereby granted, free of charge, to any person obtaining a copy
    * of this software and associated documentation files (the "Software"), to deal
    * in the Software without restriction, including without limitation the rights
    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    * copies of the Software, and to permit persons to whom the Software is
    * furnished to do so, subject to the following conditions:
    *
    * The above copyright notice and this permission notice shall be included in
    * all copies or substantial portions of the Software.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    * THE SOFTWARE.
    */

    package xxx.xx.xxxxxx.xxxxx.util;

    import tk.mybatis.mapper.common.Mapper;
    import tk.mybatis.mapper.common.MySqlMapper;

    /**
    * 继承自己的 MyMapper
    */
    public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
    //TODO
    //FIXME 特别注意,该接口不能被扫描到,否则会出错
    } 

    注意:可以使用google浏览器的postman插件  或者 火狐浏览器的RestClient插件来进行接口的测试工作。输入地址的时候要确保和配置文件里的端口号对应。

    总结:第一次开发接口,能顺利的搞出来真的是挺开心的,以前的时候就在好奇,经常听接口开发之类的,也会有疑问 如果自己来开发接口的话能不能开发出来。

    相关注解详细参考:https://blog.csdn.net/xiaojin21cen/article/details/78654652

  • 相关阅读:
    人脸识别-常用的数据库Face Databases From Other Research Groups
    447. Number of Boomerangs
    356. Line Reflection
    149. Max Points on a Line
    279. Perfect Squares
    264. Ugly Number II
    204. Count Primes
    263. Ugly Number
    202. Happy Number
    4. Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/xuchao0506/p/10249496.html
Copyright © 2020-2023  润新知