feign post get

消费Consumer端client

package com.example.order.controller;

import com.example.order.client.ProductClient;
import com.example.eurekaclient.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
public class ClientController {
    @GetMapping("/getProductMsg")
    public String getProductMsg(){
        RestTemplate restTemplate=new RestTemplate();
        String response =restTemplate.getForObject("http://10.130.24.111:8083/msg",String.class);
        log.info("response={}",response);
        return response;
    }
    @Autowired
    private LoadBalancerClient loadBalanceClient;
//    http://localhost:8081/getProductMsg2
    @GetMapping("/getProductMsg2")
    public String getProductMsg2(){
        RestTemplate restTemplate=new RestTemplate();
        ServiceInstance serverInstance=loadBalanceClient.choose("eurekaClientProduct");

        String url=String.format("http://%s:%s",serverInstance.getHost(),serverInstance.getPort()+"/msg");
        log.info("------url="+url);
        String response =restTemplate.getForObject(url,String.class);
        log.info("response={}",response);
        return response;
    }

    @Autowired
    public ProductClient productClient;
    @GetMapping("/getProductMsg4")
    public String getProductMsg4(){

        String response =productClient.productMsg();
        log.info("response={}",response);
        return response;
    }
//   http://localhost:8081/getProductList?id=123456
@RequestMapping(value = "/getProductList", method = RequestMethod.POST)
    public  List< ProductInfo> getProductList(@RequestParam String id){
//        List< ProductInfo> list= productClient.listForOrder3(Arrays.asList("123456","123457"));
        List< ProductInfo> list= productClient.getProductList(id);
        log.info("response={}",list );
        return list;
    }
//  POST http://localhost:8081/getProductList3?id=123456
    @RequestMapping(value = "/getProductList3", method = RequestMethod.POST)
    public  List< ProductInfo> getProductList3(@RequestParam String id){
        List< ProductInfo> list= productClient.getProductList3(Arrays.asList(id));
        log.info("response={}",list );
        return list;
    }

    //  GET http://localhost:8081/listForOrder2?id=123456
    @RequestMapping(value = "/listForOrder2", method = RequestMethod.GET)
    public  List< ProductInfo> listForOrder2(@RequestParam String id){
        List< ProductInfo> list= productClient.listForOrder2(Arrays.asList(id));
        log.info("response={}",list );
        return list;
    }
}

 

------------

package com.example.order.client;

import com.example.eurekaclient.ProductInfo;
import org.springframework.cloud.netflix.feign.FeignClient;
//import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@FeignClient(name="EUREKAPROVIDER")
public interface ProductClient {
//    @GetMapping("/msg")
      @RequestMapping(value = "/msg", method = RequestMethod.GET)
      String productMsg();
// POST
    @RequestMapping(value = "/product/posttest", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE )
    List<ProductInfo> getProductList(@RequestBody String  id);
 // POST
    @RequestMapping(value = "/product/listForOrder3", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE )
    List<ProductInfo> getProductList3(@RequestBody  List<String> productIdList);
//GET
    @RequestMapping(value = "/product/listForOrder2", method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_VALUE )
    List<ProductInfo> listForOrder2(@RequestBody  List<String> productIdList);


}

 

--------------------

provider端

package com.example.eurekaclient.controller;

import com.example.eurekaclient.dataobject.ProductCategory;
import com.example.eurekaclient.dataobject.ProductInfo;
import com.example.eurekaclient.service.CategoryService;
import com.example.eurekaclient.service.ProductService;
import com.example.eurekaclient.vo.ProductInfoVO;
import com.example.eurekaclient.vo.ProductVO;
import com.example.eurekaclient.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    private static final Logger LOG = LoggerFactory.getLogger(ProductController.class);
      @Autowired
      private ProductService productService;
      @Autowired
      private CategoryService categoryService;
/**
 * 1查询所有商品
 * 2获取type列表
 * 3查询类目
 * 4构造数据
 * */
      @GetMapping("/list")
      public ResultVO<ProductVO>list(){
            //1 查询商品列表
            List<ProductInfo> productInfoList=productService.findUpAll();
//  [{
//              "productId": "123456",
//                      "productName": "皮蛋粥",
//                      "productPrice": 6.67,
//                      "productStock": 55,
//                      "productDescription": "很好喝的粥",
//                      "productIcon": "http://img3.imgtn.bdimg.com/it/u=3648971941,3500022642&fm=27&gp=0.jpg",
//                      "productStatus": 0,
//                      "categoryType": 1,
//                      "createTime": "2018-08-05T00:18:37.000+0000",
//                      "updateTime": "2018-08-11T23:29:38.000+0000"
//          }, {
//              "productId": "123457",
//                      "productName": "皮皮虾123",
//                      "productPrice": 3.20,
//                      "productStock": 102,
//                      "productDescription": "很好吃的虾",
//                      "productIcon": "https://cdn.haochu.com/d/file/20131120/efda5f1bfa2f1f8ab452837495447380.jpg",
//                      "productStatus": 0,
//                      "categoryType": 1,
//                      "createTime": "2018-08-05T00:17:37.000+0000",
//                      "updateTime": "2018-08-05T00:40:39.000+0000"
//          }, {
//              "productId": "123458",
//                      "productName": "芒果冰",
//                      "productPrice": 20.00,
//                      "productStock": 204,
//                      "productDescription": "冰冰的很爽",
//                      "productIcon": "http://i5.xiachufang.com/image/620/5febb4029f2911e38844b8ca3aeed2d7.jpg",
//                      "productStatus": 0,
//                      "categoryType": 2,
//                      "createTime": "2018-07-29T02:31:33.000+0000",
//                      "updateTime": "2018-08-05T00:39:25.000+0000"
//          }, {
//              "productId": "1533382637167627934",
//                      "productName": "潮汕牛肉丸",
//                      "productPrice": 40.00,
//                      "productStock": 100,
//                      "productDescription": "好吃不上火",
//                      "productIcon": "http://imgsrc.baidu.com/image/c0%3Dpixel_huitu%2C0%2C0%2C294%2C40/sign=90fa433207d79123f4ed9c34c44c3cee/e4dde71190ef76c6fd95e8119616fdfaaf516740.jpg",
//                      "productStatus": 0,
//                      "categoryType": 1,
//                      "createTime": "2018-08-05T00:37:17.000+0000",
//                      "updateTime": "2018-08-05T00:43:37.000+0000"
//          }, {
//              "productId": "1533382895077138814",
//                      "productName": "北京烤鸭",
//                      "productPrice": 66.00,
//                      "productStock": 600,
//                      "productDescription": "打鸭子",
//                      "productIcon": "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4160105330,3932180633&fm=27&gp=0.jpg",
//                      "productStatus": 0,
//                      "categoryType": 2,
//                      "createTime": "2018-08-05T00:41:35.000+0000",
//                      "updateTime": "2018-08-05T00:41:35.000+0000"
//          }]


            //2 类目type列表[拿到某一表某一列的数值  返回数组]
            List<Integer> categoryTypeList=productInfoList.stream()
                    .map(ProductInfo::getCategoryType)
                    .collect(Collectors.toList());
//  [1, 1, 2, 1, 2]


            //3 数据库查询类目  [给定几个字段 查出表]
           List<ProductCategory> categoryList=categoryService.findByCategoryTypeIn(categoryTypeList);
// [{
//              "categoryId": 2,
//                      "categoryName": "男生最爱",
//                      "categoryType": 1,
//                      "createTime": "2018-07-28T15:07:55.000+0000",
//                      "updateTime": "2018-07-29T02:30:16.000+0000"
//          }, {
//              "categoryId": 3,
//                      "categoryName": "女生最爱",
//                      "categoryType": 2,
//                      "createTime": "2018-07-28T15:30:10.000+0000",
//                      "updateTime": "2018-08-05T01:45:37.000+0000"
//          }]

//           4 构造数据
            List<ProductVO> productVOList=new ArrayList<>();
            for(ProductCategory productCategory:categoryList){
                ProductVO productVO=new ProductVO();
                productVO.setCategoryName(productCategory.getCategoryName());
                productVO.setCategoryType(productCategory.getCategoryType());
                List<ProductInfoVO> productInfoVOList=new ArrayList<>();
                for(ProductInfo productInfo:productInfoList){
                      if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                            ProductInfoVO productInfoVO=new ProductInfoVO();
                            BeanUtils.copyProperties(productInfo,productInfoVO);
                            productInfoVOList.add(productInfoVO);
                      }
                }
                  productVO.setProductInfoVOList(productInfoVOList);
                  productVOList.add(productVO);
            }
               ResultVO resultVO=new ResultVO();
             resultVO.setData(productVOList);
             resultVO.setCode(0);
             resultVO.setMsg("success");
            return resultVO;
          /**
//             {
//             "code": 0,
//             "msg": "success",
//             "data": [{
//             "name": "男生最爱",
//             "type": 1,
//             "foods": [{
//             "id": "123456",
//             "name": "皮蛋粥",
//             "price": 6.67,
//             "description": null,
//             "icon": "http://img3.imgtn.bdimg.com/it/u=3648971941,3500022642&fm=27&gp=0.jpg"
//             }, {
//             "id": "123457",
//             "name": "皮皮虾123",
//             "price": 3.20,
//             "description": null,
//             "icon": "https://cdn.haochu.com/d/file/20131120/efda5f1bfa2f1f8ab452837495447380.jpg"
//             }, {
//             "id": "1533382637167627934",
//             "name": "潮汕牛肉丸",
//             "price": 40.00,
//             "description": null,
//             "icon": "http://imgsrc.baidu.com/image/c0%3Dpixel_huitu%2C0%2C0%2C294%2C40/sign=90fa433207d79123f4ed9c34c44c3cee/e4dde71190ef76c6fd95e8119616fdfaaf516740.jpg"
//             }]
//             }, {
//             "name": "女生最爱",
//             "type": 2,
//             "foods": [{
//             "id": "123458",
//             "name": "芒果冰",
//             "price": 20.00,
//             "description": null,
//             "icon": "http://i5.xiachufang.com/image/620/5febb4029f2911e38844b8ca3aeed2d7.jpg"
//             }, {
//             "id": "1533382895077138814",
//             "name": "北京烤鸭",
//             "price": 66.00,
//             "description": null,
//             "icon": "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4160105330,3932180633&fm=27&gp=0.jpg"
//             }]
//             }]
//             }

             **/
      }

     private ResultVO getDefault(Object data){
         ResultVO resultVO=new ResultVO();
         resultVO.setData(data);
         resultVO.setCode(0);
         resultVO.setMsg("success");
         return resultVO;
     }


/*
* 获取商品列表(订单用)
*
* */
//    @GetMapping("/listForOrder")
//@PostMapping("/listForOrder")
//    get请求
     @RequestMapping(value = "/listForOrder", method = RequestMethod.GET)
    public List<ProductInfo> listForOrder(@RequestParam   List<String> productIdList)throws Exception{
        log.info("---------listForOrder");
        return productService.findList(productIdList);
    }
//    GET:   http://localhost:8083/product/listForOrder2?id=123456
    @RequestMapping(value = "/listForOrder2", method = RequestMethod.GET)
    public List<ProductInfo> listForOrder2(@RequestBody   List<String> productIdList)throws Exception{
//        List<String> productIdList=Arrays.asList(""+id);
        log.info("---------listForOrder2 idArrays="+productIdList);
        return productService.findList(productIdList);
    }
//    POST http://localhost:8083/product/posttest?id=123456
//    @RequestMapping(value = "/posttest", method = RequestMethod.POST)
    @PostMapping(value = "/posttest")
    public List<ProductInfo> postTest(@RequestBody   String id)throws Exception{
        log.info("---------listForOrder id="+id);
        List<String> productIdList=Arrays.asList(""+id);

        return productService.findList(productIdList);
    }
    @RequestMapping(value = "/listForOrder3", method = RequestMethod.POST)
    public List<ProductInfo> listForOrder3(@RequestBody   List<String> productIdList)throws Exception{
        log.info("---------listForOrder3");
        return productService.findList(productIdList);
    }

}

 

------

链接:https://pan.baidu.com/s/1uyayYIuCjUfHCtPDNSv1MA 
提取码:0zv6 
复制这段内容后打开百度网盘手机App,操作更方便哦

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值