【SpringCloud】微服务前置知识点:restful API dependencies和dependencyManager的区别

1.Restful Api

1.1介绍

1.Representational State Transfer 直接翻译:表现层状态转移
2.REST描述的是在网络中client和server的一种交互形式;REST本身不实用,实用的是如何设计 RESTful API(REST风格的网络接口);
3.Server提供的RESTful API中,URL中只使用名词来指定资源,原则上不使用动词。“资源”是REST架构或者说整个网络处理的核心
在这里插入图片描述

1.2 以前URL与Rest的URL的区别

以前的URL,结尾一般是动词来体现动作

http://localhost:8080/demo01/person/add.do(动词,体现动作)

现在的Restful的URL,结尾是资源,比如id的值, 屏蔽了因为语言的不同,而导致多个系统之间不统一,就不要考虑带不带jsp,或者php后缀

根据请求方式来区分来表示不同的动作
method = {RequestMethod.GET}) 查询/获取资源
method = {RequestMethod.POST}) 新建资源(也可以用于更新资源)
method = {RequestMethod.DELETE}) 删除资源
method = {RequestMethod.PUT}) 更新资源
在这里插入图片描述

http://localhost:8080/demo01/person/newuser(名词 体现资源)

对比代码

{id} {}表示在地址上的占位符 与# $类似
@PathVariable注解 表示取地址上的值 如@PathVariable int id
特别提醒
一般path = “user/{id}” 与 @PathVariable int id 要字段要相同都是id
如果不相同 可以@PathVariable(“id”) int userid 来补救

package com.zx.demo01restful.controller;

import com.zx.demo01restful.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UserController {

    @RequestMapping(path = "user/find.do",method = {RequestMethod.GET})
    public Object test01(int id){
       User user= new User();
       user.setId(id);
       user.setUsername("jack");
       user.setPassword("123456");
       return user;
    }

    @RequestMapping(path = "user/{id}",method = {RequestMethod.GET})
    public Object test02(@PathVariable int id){
        log.info("取到的id为:"+id);
        User user= new User();
        user.setId(id);
        user.setUsername("jack");
        user.setPassword("123456");
        return user;
    }

    @RequestMapping(path = "user/{id}",method = {RequestMethod.DELETE})
    public Object test03(@PathVariable int id){
        log.info("删除了id:"+id);

        User user= new User();
        user.setId(id);
        user.setUsername("jack");
        user.setPassword("123456");
        return user;
    }
}

1.3 RestTemplate

RestTemplate类似postman 可以模拟http请求

介绍

1.spring 提供了RestTemplate的工具类对上述的3种http客户端工具类进行了封装,可在spring项目中使用RestTemplate进行服务调用。
2.三种http客户端工具类包都
httpClient apache
okHttp
JDK原生URLConnection java
3.使用restTemplate访问restful接口非常的简单粗暴无脑。

请求地址 + 请求方式
请求参数
返回值
使用restTemplate访问restful接口非常的简单粗暴无脑。
postForObject(url, requestMap, ResponseBean.class)
参1 请求地址
参2 请求参数
参3 HTTP响应转换被转换成的对象类型
在这里插入图片描述

Demo02restTemplateApplicationTests

package com.zx.demo02rest_template;

import com.zx.demo02rest_template.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;

@SpringBootTest
@Slf4j
class Demo02restTemplateApplicationTests {

    @Test
    void test01() {
        RestTemplate template=new RestTemplate();
        template.delete("http://localhost:8080/user/3");
    }

    @Test
    void test02() {
        RestTemplate template=new RestTemplate();
        User user=template.getForObject("http://localhost:8080/user/3", User.class);
        log.info("user对象:"+user);
    }
}

2.dependencies和dependencyManager的区别(dependencyManager用于消费方,来获取提供者的方法所返回的值)

2.1dependencise

是pom.xml元素,表示依赖项的集合
在父子工程中使用,所有生命在dependencies里的依赖都会自动引入,并默认被所有的子项目继承。

2.2dependencyManager

**用于管理jar包的版本,**让子项目中引用一个依赖而不用显示的列出版本号。

原理:Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号
但是子项目还得写依赖,但不写版本 本质上是声明依赖与版本(只是说有这个东西,但没有真正加进来)
父工程

  <!--声明(锁定jar的版本)mysql,但是不将mysql引入到父工程中 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.46</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

子工程不用写版本号

 <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

2.3区别

(1)dependencies即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)
(2)dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。
如果不在子项目中声明依赖,是不会从父项目中继承下来的;
只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;
另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值