SpringBoot学习笔记下

1 接口架构风格 —RESTful

接口: API(Application Programming Interface,应用程序接口)是一些预先定义的接口(如函数、HTTP接口),或指软件系统不同组成部分衔接的约定。 用来提供应用程序与开发人员基于某软件或硬件得以访问的一组例程,而又无需访问源码,或理解内部工作机制的细节。

接口(API): 可以指访问servlet, controller的url, 调用其他程序的函数

架构风格: api组织方式(样子)
就是一个传统的: http://localhost:8080/myboot/addStudent?name=lisi&age=26
在地址上提供了 访问的资源名称addStudent, 在其后使用了get方式传递参数。

1.1 REST

RESTful架构风格

  1. REST : (Representational State Transfer , 中文: 表现层状态转移)。

    REST:是一种接口的架构风格和设计的理念,不是标准。

    优点: 更简洁,更有层次

    表现层状态转移:

     表现层就是视图层, 显示资源的, 通过视图页面,jsp等等显示操作资源的结果。
    
     状态: 资源变化
    
     转移: 资源可以变化的。 资源能创建,new状态,  资源创建后可以查询资源, 能看到资源的内容,这个资源内容 ,可以被修改, 修改后资源和之前的不一样。  
    
  2. REST中的要素:

    用REST表示资源和对资源的操作。 在互联网中,表示一个资源或者一个操作。

    资源使用url表示的, 在互联网, 使用的图片,视频, 文本,网页等等都是资源。

    资源是用名词表示。

    对资源:

     查询资源: 看,通过url找到资源。 
    
     创建资源: 添加资源
    
     更新资源:更新资源 ,编辑
    
     删除资源: 去除
    

    资源使用url表示,通过名词表示资源。

    在url中,使用名词表示资源, 以及访问资源的信息, 在url中,使用“ / " 分隔对资源的信息

    http://localhost:8080/myboot/student/1001

    使用http中的动作(请求方式), 表示对资源的操作(CURD)

    GET: 查询资源 – sql select

     处理单个资源: 用他的单数方式
     http://localhost:8080/myboot/student/1001
     http://localhost:8080/myboot/student/1001/1
     处理多个资源:使用复数形式
     http://localhost:8080/myboot/students/1001/1002
    

    POST: 创建资源 – sql insert

     http://localhost:8080/myboot/student
     在post请求中传递数据
    
    <form action="http://localhost:8080/myboot/student" method="post">
    	姓名:<input type="text" name="name" />
        年龄:<input type="text" name="age" />
      </form>
    

    PUT: 更新资源 – sql update

    <form action="http://localhost:8080/myboot/student/1" method="post">
    	姓名:<input type="text" name="name" />
        年龄:<input type="text" name="age" />
             <input type="hidden" name="_method" value="PUT" />
    </form>
    

    DELETE: 删除资源 – sql delete

    <a href="http://localhost:8080/myboot/student/1">删除1的数据</a>
    

    需要的分页, 排序等参数,依然放在 url的后面, 例如

     http://localhost:8080/myboot/students?page=1&pageSize=20
    
  3. 一句话说明REST:

    使用url表示资源 ,使用http动作操作资源。

  4. 注解

    @PathVariable : 从url中获取数据

    @GetMapping: 支持的get请求方式,
    等同于 @RequestMapping( method=RequestMethod.GET)

    @PostMapping: 支持post请求方式 ,
    等同于 @RequestMapping( method=RequestMethod.POST)

    @PutMapping: 支持put请求方式,
    等同于 @RequestMapping( method=RequestMethod.PUT)

    @DeleteMapping: 支持delete请求方式
    等同于@RequestMapping(method=RequestMethod.DELETE)

    @RestController: 复合注解, 是@Controller 和@ResponseBody组合。
    在类的上面使用 @RestController , 表示当前类者的所有方法都加入了 @ResponseBody

1.2 在页面中或者ajax中,支持put,delete请求

在SpringMVC中 有一个过滤器, 支持post请求转为put ,delete

过滤器: org.springframework.web.filter.HiddenHttpMethodFilter

作用: 把请求中的post请求转为 put , delete

实现步骤:

  1. application.properties(yml) : 开启使用 HiddenHttpMethodFilter 过滤器

    # 启用支持put、delete
    spring.mvc.hiddenmethod.filter.enabled=true
    
  2. 在请求页面中,包含 _method参数, 他的值是 put, delete , 发起这个请求使用的post方式

    <form action="student/test" method="post">
        <input type="hidden" name="_method" value="put">
    </form>
    

2 Spring Boot 集成 Redis

Redis : 一个NoSQL数据库, 常用作 缓存使用 (cache)

Redis的数据类型: string , hash ,set ,zset , list

Redis是一个中间件: 是一个独立的服务器。

java中著名的客户端: Jedis , lettuce , Redisson

Spring,SpringBoot中有 一个RedisTemplate(StringRedisTemplate) ,处理和redis交互

2.1 redis起步依赖

RedisTemplate 使用的 lettuce 客户端库

<!--redis起步依赖: 直接在项目中使用RedisTemplate(StringRedisTemplate)-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
data-redis使用的   lettuce 客户端库

在程序中使用RedisTemplate类的方法 操作redis数据, 实际就是调用的lettuce 客户端的中的方法

2.2 对比 StringRedisTemplate 和 RedisTemplate

StringRedisTemplate : 把k,v 都是作为String处理, 使用的是String的序列化 , 可读性好

RedisTemplate : 把k,v 经过了序列化存到redis。 k,v 是序列化的内容, 不能直接识别.默认使用的jdk序列化

序列化:把对象转化为可传输的字节序列过程称为序列化。

反序列化:把字节序列还原为对象的过程称为反序列化。

为什么需要序列化
序列化最终的目的是为了对象可以跨平台存储,和进行网络传输。而我们进行跨平台存储和网络传输的方式就是IO,而我们的IO支持的数据格式就是字节数组。我们必须在把对象转成字节数组的时候就制定一种规则(序列化),那么我们从IO流里面读出数据的时候再以这种规则把对象还原回来(反序列化)。

什么情况下需要序列化
通过上面我想你已经知道了凡是需要进行“跨平台存储”和”网络传输”的数据,都需要进行序列化。

本质上存储和网络传输 都需要经过 把一个对象状态保存成一种跨平台识别的字节格式,然后其他的平台才可以通过字节信息解析还原对象信息。

序列化的方式

序列化只是一种拆装组装对象的规则,那么这种规则肯定也可能有多种多样,比如现在常见的序列化方式有:

JDK(不支持跨语言)、JSON、XML、Hessian、Kryo(不支持跨语言)、Thrift、Protofbuff、

java的序列化: 把java对象转为byte[], 二进制数据

json序列化:json序列化功能将对象转换为 JSON 格式或从 JSON 格式转换对象。例如把一个Student对象转换为JSON字符串{“name”:“李四”, “age”:29} ),反序列化(将JSON字符串 {“name”:“李四”, “age”:29} 转换为Student对象)
Student( name=zs, age=20) ---- { “name”:“zs”, “age”:20 }

设置key或者value的序列化方式

// 使用RedisTemplate ,在存取值之前,设置序列化
// 设置 key 使用String的序列化
redisTemplate.setKeySerializer( new StringRedisSerializer());

// 设置 value 的序列化
redisTemplate.setValueSerializer( new StringRedisSerializer());

redisTemplate.opsForValue().set(k,v);

3 SpringBoot集成Dubbo

3.1 看 SpringBoot继承Dubbo的文档

https://github.com/apache/dubbo-spring-boot-project/blob/master/README_CN.md

3.2 公共项目

独立的maven项目: 定义了接口和数据类

public class Student implements Serializable {
    private static final long serialVersionUID = 1901229007746699151L;

    private Integer id;
    private String name;
    private Integer age;
    // set、get、toString方法
}
public interface StudentService {

    Student queryStudent(Integer id);
}

7.3 提供者

创建SpringBoot项目

1) pom.xml

<dependencies>

   <!--加入公共项目的gav-->
   <dependency>
      <groupId>com.yrgdcx</groupId>
      <artifactId>yrgdcx-interface-api</artifactId>
      <version>1.0.0</version>
   </dependency>

   <!--dubbo依赖-->
   <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
      <version>2.7.8</version>
   </dependency>

   <!--zookeeper依赖-->
   <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-dependencies-zookeeper</artifactId>
      <version>2.7.8</version>
      <type>pom</type>
      <exclusions>
         <!-- 排除log4j依赖 -->
         <exclusion>
            <artifactId>slf4j-log4j12</artifactId>
            <groupId>org.slf4j</groupId>
         </exclusion>
      </exclusions>
   </dependency>
</dependencies>

2)实现接口

/**
 * 使用dubbo中的注解暴露服务
 * @Component 可以不用加
 */
@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)
public class StudentServiceImpl implements StudentService {
    @Override
    public Student queryStudent(Integer id) {
        Student student  = new Student();
        if( 1001 == id){
            student.setId(1001);
            student.setName("------1001-张三");
            student.setAge(20);
        } else if(1002  == id){
            student.setId(1002);
            student.setName("#######1002-李四");
            student.setAge(22);
        }
        return student;
    }
}

3)application.properties

#配置服务名称 dubbo:application name="名称"
spring.application.name=studentservice-provider

#配置扫描的包, 扫描的@DubboService
dubbo.scan.base-packages=com.yrgdcx.service

#配置dubbo协议
#dubbo.protocol.name=dubbo
#dubbo.protocol.port=20881

#注册中心
dubbo.registry.address=zookeeper://localhost:2181

4)在启动类的上面

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {

   public static void main(String[] args) {
      SpringApplication.run(ProviderApplication.class, args);
   }
}

7.4消费者

创建SpringBoot项目

1) pom.xml

<dependencies>

   <!--加入公共项目的gav-->
   <dependency>
      <groupId>com.yrgdcx</groupId>
      <artifactId>yrgdcx-interface-api</artifactId>
      <version>1.0.0</version>
   </dependency>

   <!--dubbo依赖-->
   <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
      <version>2.7.8</version>
   </dependency>

   <!--zookeeper依赖-->
   <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-dependencies-zookeeper</artifactId>
      <version>2.7.8</version>
      <type>pom</type>
      <exclusions>
         <!-- 排除log4j依赖 -->
         <exclusion>
            <artifactId>slf4j-log4j12</artifactId>
            <groupId>org.slf4j</groupId>
         </exclusion>
      </exclusions>
   </dependency>
</dependencies>
  1. 创建了Controller
@RestController
public class DubboController {
    /**
     * 引用远程服务, 把创建好的代理对象,注入给studentService
     */
    //@DubboReference(interfaceClass = StudentService.class,version = "1.0")
    /**
     * 没有使用interfaceClass,默认的就是 引用类型的 数据类型
      */
    @DubboReference(version = "1.0")
    private StudentService studentService;

    @GetMapping("/query")
    public String queryStudent(Integer id){
        Student student   = studentService.queryStudent(id);
        return "调用远程接口,获取对象:"+student;
    }
}

3)application.properties

#指定服务名称
spring.application.name=consumer-application
#指定注册中心
dubbo.registry.address=zookeeper://localhost:2181

注解总结

Spring + SpringMVC + SpringBoot

创建对象的:
@Controller: 放在类的上面,创建控制器对象,注入到容器中
@RestController: 放在类的上面,创建控制器对象,注入到容器中。
             作用:复合注解是@Controller , @ResponseBody, 使用这个注解类的,里面的控制器方法的返回值                   都是数据

@Service : 放在业务层的实现类上面,创建service对象,注入到容器
@Repository : 放在dao层的实现类上面,创建dao对象,放入到容器。 没有使用这个注解,是因为现在使用MyBatis框               架,  dao对象是MyBatis通过代理生成的。 不需要使用@Repository、 所以没有使用。
@Component:  放在类的上面,创建此类的对象,放入到容器中。 

赋值的:
@Value : 简单类型的赋值, 例如 在属性的上面使用@Value("李四") private String name
          还可以使用@Value,获取配置文件者的数据(properties或yml)。 
          @Value("${server.port}") private Integer port

@Autowired: 引用类型赋值自动注入的,支持byName, byType. 默认是byType 。 放在属性的上面,也可以放在构造             方法的上面。 推荐是放在构造方法的上面
@Qualifer:  给引用类型赋值,使用byName方式。   
            @Autowird, @Qualifer都是Spring框架提供的。

@Resource : 来自jdk中的定义, javax.annotation。 实现引用类型的自动注入, 支持byName, byType.
             默认是byName, 如果byName失败, 再使用byType注入。 在属性上面使用


其他:
@Configuration : 放在类的上面,表示这是个配置类,相当于xml配置文件

@Bean:放在方法的上面, 把方法的返回值对象,注入到spring容器中。

@ImportResource : 加载其他的xml配置文件, 把文件中的对象注入到spring容器中

@PropertySource : 读取其他的properties属性配置文件

@ComponentScan: 扫描器 ,指定包名,扫描注解的

@ResponseBody: 放在方法的上面,表示方法的返回值是数据, 不是视图
@RequestBody : 把请求体中的数据,读取出来, 转为java对象使用。

@ControllerAdvice:  控制器增强, 放在类的上面, 表示此类提供了方法,可以对controller增强功能。

@ExceptionHandler : 处理异常的,放在方法的上面

@Transcational :  处理事务的, 放在service实现类的public方法上面, 表示此方法有事务


SpringBoot中使用的注解
    
@SpringBootApplication : 放在启动类上面, 包含了@SpringBootConfiguration
                          @EnableAutoConfiguration@ComponentScan


    
MyBatis相关的注解

@Mapper : 放在类的上面 , 让MyBatis找到接口, 创建他的代理对象    
@MapperScan :放在主类的上面 , 指定扫描的包, 把这个包中的所有接口都创建代理对象。 对象注入到容器中
@Param : 放在dao接口的方法的形参前面, 作为命名参数使用的。
    
Dubbo注解
@DubboService: 在提供者端使用的,暴露服务的, 放在接口的实现类上面
@DubboReference:  在消费者端使用的, 引用远程服务, 放在属性上面使用。
@EnableDubbo : 放在主类上面, 表示当前引用启用Dubbo功能。
    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值