首页 > 其他 > 详细

Rest,服务消费者

时间:2020-11-29 23:40:43      阅读:52      评论:0      收藏:0      [点我收藏+]

Rest服务消费者

编写服务消费者模块

  1. 创建消费者,一般指定端口号为80
    技术分享图片
  2. pom.xml和application.yml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>cn.lzm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-consumer-person-80</artifactId>
    <dependencies>
        <!--引入api模块-->
        <dependency>
            <groupId>cn.lzm</groupId>
            <artifactId>API</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>

    </dependencies>

</project>
server:
  port: 80
  1. 将RestTemplate注册到spring IOC容器中
package cn.lzm.springcloud.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class WebConfig {
    
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

  1. 编写控制器,消费者不应该有服务层
package cn.lzm.springcloud.controller;
import cn.lzm.springcloud.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 理解:消费者,不应该有service层
 * RestTemplate... 直接调用注册到spring中
 * (String url, Class<T> responseType, Map<String, ?> uriVariables)路径 响应类字节码 map请求参数
 */
@Controller
public class PersonController {

    @Autowired
    RestTemplate restTemplate;
    private static final String REQUEST_URL = "http://localhost:8001";

    @GetMapping("/person/com/add")
    @ResponseBody
    public String addPerson(Person person){
        //postForObject ,getForObjec ...
        return restTemplate.postForObject(REQUEST_URL+"/person/add",person,String.class);
    }

    @GetMapping("/person/com/get/{id}")
    @ResponseBody
    public Person getPersonById(@PathVariable("id") int id){
        return restTemplate.getForObject(REQUEST_URL+"/person/{id}",Person.class,id);
    }
    @GetMapping("/person/com/get/all")
    @ResponseBody
    public List<Person> getPersonAll(){
        return restTemplate.getForObject(REQUEST_URL+"/person/getall",List.class);
    }

}

  1. 测试一下
  • 成功()
    技术分享图片

Rest,服务消费者

原文:https://www.cnblogs.com/xiaominaaaa/p/14058588.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!