SpringBoot03_MVC的使用

一、在 SpringBoot 中使用 REST

​ 为演示模块之间的调用,所以创建了 pom 工程,在 maven 工程中添加各种 starter 来搭建 SpringBoot 项目。

image-20230517184520741

(一)自定义返回一个 LsResult 的返回值,并将其打包为其他模块的依赖

package com.qlu.vo;
@Data
@Accessors(chain = true)
public class LsResult {
    private Integer code;
    private Object data;
}
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

(二)编写 Controller 部分内容是和 SpringMVC 一样的

package com.qlu.controller;
@RestController
@RequestMapping("/book")
public class BookController {

    @GetMapping("/{id}")
    public LsResult getBook(@PathVariable Integer id){
        System.out.println("查找图书:"+id);
        return new LsResult().setCode(200);
    }

    @PostMapping("/")
    public LsResult addBook(){
        System.out.println("添加图书:");
        return new LsResult().setCode(200);
    }

    @PutMapping("/")
    public LsResult updateBook(){
        System.out.println("修改图书");
        return new LsResult().setCode(200);
    }

    @DeleteMapping("/")
    public LsResult deleteBook(){
        System.out.println("删除图书");
        return new LsResult().setCode(200);
    }
}

image-20230517185231380

image-20230517185255679

image-20230517185312385

二、远程调用 REST http接口

​ 模块之间可以相互调用,而之间调用一般使用 HTTP 协议。我们以在 pom 工程的两个模块 boot1 和 boot2 中演示,其中 boot1 使用接口8080,内部有 /hello/say;boot2 使用8081端口,我们需要让 boot2 调用boot 一中的 /hello/say。

(一)HttpClient 方式

​ httpclient 是 Apache Jakarta Common下的子项目,用来提供高效的、最新的、更能丰富的支持 http 协议的客户端编程工具包,并且它支持http协议最新的版本和建议。

​ 使用的是基于 hutool-all 工具包的 HttpUtil ,其实还是 HttpClient,可以看到内部有多种请求方式。

@RestController
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/say1")
    public String say1(){
        String result = HttpUtil.post("http://localhost:8080/boot1/hello/say", "");
        return result;
    }
    }

image-20230517190421761

image-20230517190126493

image-20230517190342997

(二)RestTmplete

​ RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。

​ RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful 操作,这些操作在 RestTemplate 中都得到了实现。传统情况下在java代码里访问Restful服务,一般使用Apache的HttpClient。不过此种方法使用起来太繁琐。Spring提供了一种简单便捷的模板类RestTemplate来进行操作。

​ 在 SpringBoot 中使用 RestTemplate 需要先创建 RestTemplate 的实例将其放到容器中,我们将其放在 config 包下。

package com.qlu.config;
@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

​ 同样的我们想通过访问 boot2 中的 say2 达到访问 boot1 模块中的 say。

package com.qlu.controller;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/say2")
    public Map say2(){
        Map map = restTemplate.postForObject("http://localhost:8080/boot1/hello/say", null, Map.class);
        return map;
    }
}

image-20230518150247777

image-20230518150337724

posted @ 2023-05-18 15:16  Purearc  阅读(27)  评论(0编辑  收藏  举报