首页 > 其他 > 详细

使用RestTemplate跨服调用

时间:2019-11-07 22:35:48      阅读:99      评论:0      收藏:0      [点我收藏+]

添加所需依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

module01的service层:

ProductService:

import com.hui.springcloud.entity.Product;

import java.util.List;

public interface ProductService {

    Product getProductByPid(Integer pid);

    List<Product> getAllProduct();

    boolean addProduct(Product product);
}

ProductServiceImpl:

import com.hui.springcloud.entity.Product;
import com.hui.springcloud.mapper.ProductMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public Product getProductByPid(Integer pid) {
        return productMapper.getProductByPid(pid);
    }

    @Override
    public List<Product> getAllProduct() {
        return productMapper.getAllProduct();
    }

    @Override
    public boolean addProduct(Product product) {
        return productMapper.addProduct(product);
    }
}

在module02编写配置类CrossServiceCallConfig.java

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

@Configuration
// 跨服调用配置类
public class CrossServiceCallConfig {

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

module02中编写controller:

CustomerController:

import com.hui.springcloud.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping(value = "/customer")
public class ConsumerController {

    private static final String REST_URL = "http://localhost:8001/product";

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/getProductByPid/{pid}",method = RequestMethod.GET)
    public Product getProductByPid(@PathVariable("pid") Integer pid){
        Product product = restTemplate.getForObject(REST_URL + "/getProductByPid/" + pid, Product.class);
        return  product;
    }

    @RequestMapping(value = "/getAllProduct",method = RequestMethod.GET)
    public List<Product> getAllProduct(){
        List<Product> list = restTemplate.getForObject(REST_URL + "/getAllProduct", List.class);
        return list;
    }

    @RequestMapping(value = "/addProduct/{productName}",method = RequestMethod.GET)
    public boolean addProduct(@PathVariable("productName") String productName){
        Product product = new Product();
        product.setProductName(productName);
        Boolean isSuccess = restTemplate.getForObject(REST_URL + "/addProduct/" + product, Boolean.class);
        return isSuccess;
    }
}

此时可以在浏览器直接请求后台接口

使用RestTemplate跨服调用

原文:https://www.cnblogs.com/marrycode/p/11815866.html

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