java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。

开发规范:


    前后端分离:

        根据需求文档开发

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第1张图片


    Resultful风格:


        REST(REpresentational State Transfer),表述性状态转换,它是一种软件架构风格。

     java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第2张图片
POST(insert) 负责新增的操作


        http://localhost:8080/users

  DELETE(delete) 负责删除的操作

 http://localhost:8080/users/1

 
  PUT(update) 负责修改的操作

   http://localhost:8080/users


GET(select)负责查询的操作:


        http://localhost:8080/users/1

对比:

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第3张图片

更利于项目的开发和维护


    Apifox

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第4张图片


        介绍:

Apifox是一款集成了Api文档、Api调试、Api Mock、Api测试的一体化协作平台。


        作用:

接口文档管理、接口请求测试、Mock服务。

     官网: 

 https://apifox.com/

如何使用:

打开Apifox,在首页点击新建项目
  

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第5张图片

名称自定义,然后点击确定。

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第6张图片

新建快捷请求:

在进创建好的项目里面,点击快捷请求:

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第7张图片

选择对应的请求方式(get,post,delete,put):

然后输入要访问的路径

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第8张图片


查询部门案例:


    基本实现


        1,加载并读取dept.txt文本中的数据

// 读取数据
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("dept.txt");
List lines = IOUtils.readLines(inputStream, "UTF-8");

                
        2,解析文本中的数据,并将其封装到集合中

//转换到集合中
List deptList = lines.stream().map(line ->
        {
            String[] split = line.split(",");
            Integer id = Integer.valueOf(split[0]);
            String name = split[1];
            LocalDateTime updateTime = LocalDateTime.parse(split[2], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new Dept(id, name, updateTime);
        }
).toList();


        3,响应数据(json)格式

        

return deptList;

完整代码:

先创建实体类:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dept {
    private Integer id;
    private String name;
    private LocalDateTime updateTime;

}

在写DeptController

@RestController
public class DeptsController {

    @GetMapping("/depts2")
    public List getDeptList() throws IOException {
        // 读取数据
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("dept.txt");
        List lines = IOUtils.readLines(inputStream, "UTF-8");

        //转换到集合中
        List deptList = lines.stream().map(line ->
                {
                    String[] split = line.split(",");
                    Integer id = Integer.valueOf(split[0]);
                    String name = split[1];
                    LocalDateTime updateTime = LocalDateTime.parse(split[2], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                    return new Dept(id, name, updateTime);
                }
        ).toList();
        return deptList;
    }
}

效果:

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第9张图片


    统一响应结果:

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第10张图片

  •         执行成功还是失败?
  •         响应的错误提示信息
  •         响应的数据

        定义统一的Result类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    private Integer code;    //返回码 0成功 1 失败
    private String msg;     //返回描述
    private T data;         // 返回数据

    public static Result success(){
        return new Result<>(0,"操作成功",null);
    }
    public static Result success(String msg){
        return new Result<>(0,msg,null);
    }

    public static Result success(Object data){
        return new Result<>(0,"操作成功",data);
    }

    public static Result fail(){
        return new Result(1,"操作失败",null);
    }

    public static Result fail(String msg){
        return new Result(1,msg,null);
    }

}


    前后端联调测试

        nginx
            server {
   listen 90;
   #省略...
   location ^~ /api/ {
      rewrite ^/api/(.*)$ /$1 break;
      proxy_pass http://localhost:8080;
   }
}

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第11张图片 

  •                 localtion:用于定义匹配特定的url请求规则
  •                 ^~/api/ 表示精确匹配,即只可匹配以/api/开头的路径
  •                 rewrite 该指令用于重写匹配到的url路径
  •                 proxy_pass 该指令用于代理转发,他将匹配到的请求转发给位于后端的指令服务器


    三层架构:


        
        Controller  接收请求,响应数据


            controller 控制层,接收前端发送的请求,对请求进行处理,并响应数据。


        Service  逻辑处理


            service 业务逻辑层,处理具体的业务逻辑。


        Dao 数据访问


          数据访问层(Data Access Object)持久层,负责数据访问操作,包括数据的增删改查

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第12张图片​​​​​​​
        
    

定义DeptDao接口: 

 


public interface DeptDao {

    public List list();
}
定义DeptDaoImpl实现类

 

public class DeptDaoImpl implements DeptDao {

   public List list(){
       //加载文件,获取元素数据
       InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("dept.txt");
       List lines = null;
       try {
           lines = IOUtils.readLines(inputStream, "UTF-8");
       } catch (IOException e) {
           throw new RuntimeException(e);
       }
       return lines;
   }
}

定义DeptService接口:
 
public interface DeptService {

    public List getList();
}
定义DeptServiceImpl实现类:

public class DeptServiceImpl implements DeptService {

 
    private DeptDao deptDao = new DeptDaoImpl;

  public List getList(){
      List lines = deptDao.list();
      //对元素数据进行处理,组装部门数据
      List deptList = lines.stream().map(line ->
              {
                  String[] split = line.split(",");
                  Integer id = Integer.valueOf(split[0]);
                  String name = split[1];
                  LocalDateTime updateTime = LocalDateTime.parse(split[2], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                  return new Dept(id, name, updateTime);
              }
      ).toList();
      return deptList;
  }
}
定义DeptController控制器
public class DeptController {


    private DeptService deptService = new DeptServiceImpl;

    @GetMapping("/depts")
    public com.sde.utils.Result getDeptList() throws IOException {
        List deptList = deptService.getList();
        // 响应数据
        return Result.success(deptList);
    }
}
在Apifox测试:

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第13张图片

分层解耦:


        耦合:衡量软件中各个层/模块之间的依赖、关联的程度
        内聚:软件中各个功能模块内部的功能联系。

        软件设计原则:高内聚低耦合。

    Ioc和DI入门:


        将Dao及Service层的实现类,交给Ioc容器管理
        为Controller及Service注入与你形式所依赖的对象。

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第14张图片

控制反转:

Inversion Of Control,简称IOC。对象的创建控制权由程序自身转移到外部(容器),这种思想称为控制反转。

依赖注入:

Dependency Injection,简称DI。容器为应用程序提供运行时,所依赖的资源,称之为依赖注入。

Bean对象

:IOC容器中创建、管理的对象,称之为Bean


    Ico详解:
  • 前面声明bean的四大注解,要想生效,还需要被组件扫描注解@ComponentScan扫描。
  • 该注解虽然没有显式配置,但是实际上已经包含在了启动类声明注解 @SpringBootApplication 中,默认扫描的范围是启动类所在包及其子包。
java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第15张图片
TilasRun这个启动类是com.sde这个包的子类,其他的像,controller,dao,pojo,service,utils。都是com.sde的子类,因此@ComponentScan 可以扫描到。
        

例如我把TiasRun这个启动类放到,boot包下,就扫描不到了。

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第16张图片

@Component


            声明bean的基础注解,不属于以下三类时,用此注解。


        @Controller


            @Component的衍生注解,标注在控制层类上。


        @Service


            @Component 的衍生注解,标注在业务层类上


        @Repository


            @Component的衍生注解,标注在数据访问层类上(由于与mybatis整合,用的少)


        DI依赖注入的问题


            @Autowired注解,默认是按照类型进行,如果存在多个相同类型的bean,将会报出如下错误:

例如我把DeptServiceImpl,多复制一份,改名成DeptServiceImpl2

注入的时候,报警告

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第17张图片

运行然后报错了

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第18张图片


                
        解决方案:

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第19张图片

代码:

@Primary
@Primary
@Service
public class DeptServiceImpl2 implements DeptService {

    @Autowired
    private DeptDao deptDao;

  public List getList(){
      System.out.println("deptService2,222222");
      List lines = deptDao.list();
      //对元素数据进行处理,组装部门数据
      List deptList = lines.stream().map(line ->
              {
                  String[] split = line.split(",");
                  Integer id = Integer.valueOf(split[0]);
                  String name = split[1];
                  LocalDateTime updateTime = LocalDateTime.parse(split[2], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                  return new Dept(id, name, updateTime);
              }
      ).toList();
      return deptList;
  }
}

现在就可以正常运行了

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第20张图片

效果:看控制台输出的得知,现在已经用的是 deptServiceImpl2了。

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第21张图片

@Qualifoer

在·controller层注入的时候,添加@Qualifier(value="deptServiceImpl")。看控制台输出的得知,现在已经用的是 deptServiceImpl了。

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第22张图片

效果:

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第23张图片

@Resource

在·controller层注入的时候,添加@Resource(name="deptServiceImpl2")。看控制台输出的得知,现在已经用的是 deptServiceImpl2了。

效果:

java开发实战 基于Resuful风格开发接口, Ioc&Di和nginx,以及三层架构思想,分层解耦,并使用Apifox对接口数据进行测试。_第24张图片


            
        @Resource和@Autowired的区别:
  •             @Autowired是Spring框架提供的注解,而@Resource是JavaEE规范提供的
  •             @Autowired默认是按照类型注入,而@Resource默认是按照名称注入

你可能感兴趣的:(java技术成才之路,java,nginx,spring,spring,boot)