spring boot使用RestTemplate跨服调用

添加所需依赖

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

module01(PORT:8001)的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);
    }
}

ProductController:

import com.hui.springcloud.entity.Product;
import com.hui.springcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/getProductByPid/{pid}",method = RequestMethod.GET)
    public Product getProductByPid(@PathVariable("pid") Integer pid){
        return productService.getProductByPid(pid);
    }

    @RequestMapping(value = "/getAllProduct",method = RequestMethod.GET)
    public List<Product> getAllProduct(){
        return productService.getAllProduct();
    }

    @RequestMapping(value = "/addProduct",method = RequestMethod.POST)
    public void addProduct(@RequestParam Product product){
        boolean success = productService.addProduct(product);
    }
}

 

在module02(PORT:8002)编写配置类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;
    }
}

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

posted @ 2019-11-07 21:44  LadyGodiva  阅读(335)  评论(0编辑  收藏  举报