HttpClient服务器调用发送接收参数对比介绍

前言:

一直使用HttpClient做服务器调用 但是老是接收参数有问题,今天把它的常见的方法,用CRUD写了一遍 希望对你有用

1. POST©

send

  1. postForEntity(url, request就是需要保存的对象,responseType响应结果类型)

  2. postForObject(url, request就是需要保存的对象,responseType响应结果类型)

    @PostMapping("/saveproduct")
    public ResponseEntity<String>  saveProduct(Product product){
     
        String url = "http://localhost:8081/product/save";
      
        // 使用对象接收crm系统的返回
        ResponseEntity<String> entity = restTemplate.postForEntity(url,product,String.class);
        
        return entity ;
    }

get 201

  @PostMapping("/save")
    public ResponseEntity<String> saveProduct(@RequestBody Product product){
        //save
        productService.saveProduct(product);
       //201
        return new ResponseEntity<>(HttpStatus.CREATED);
    }



2. GET ®

send

  1. getForEntity(url, responseType响应结果类型) -返回响应体+响应状态码+等等
  2. getForObject(url, responseType响应结果类型) -返回响应体,会自动封装成指定的类型
  3. 如果不需要返回值,responseType=Void.class
    @GetMapping("/mailclassrid")
    public ResponseEntity<String>  findCategoryByMainClassrId(String mid){

        String url = "http://localhost:8081/category/findCategoryByMainClassrId?mid="+mid;

        ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);

  
        return entity ;
    }

get 200

   @GetMapping("/findCategoryByMainClassrId")
    public ResponseEntity<List<Category>> findCategoryByMid(String mid){

        if(mid==null){
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        List<Category> categoryByMid = categoryService.findCategoryByMid(Integer.parseInt(mid));

        return new ResponseEntity<>(categoryByMid,HttpStatus.OK);

    }

3. PUT(U)

send

  1. put(url, request就是需要保存的对象)
  @PutMapping("/updateproduct")
   public ResponseEntity<String> findProductByPid(Product product){
       String url = "http://localhost:8081/product/updateproduct";
       // 使用对象接收crm系统的返回
       restTemplate.put(url,product);
       return new ResponseEntity<>(HttpStatus.NO_CONTENT);
   }

get 204

   @PutMapping("/updateproduct")
    public void findProductByPid(@RequestBody Product product){
        if (product!=null){
             productService.updateProduct(product);
             
        }
    }

4. DELETE(D)

send

  1. delete(url)
    @DeleteMapping("/deletes")
    public void delete(String ids){
        String url = "http://localhost:8081/product/"+ids;
        try {
            restTemplate.delete(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(ids);
    }

get 204

    @DeleteMapping("/{ids}")
    public ResponseEntity<Void> deleteCourier(@PathVariable("ids") String ids){
        try {
           // System.out.println(ids);
            productService.deleteCourier(ids);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

如果上述方法传参还是传不过去则使用RequestEntity传参方法

    public void hah(Product product){
        String url="http://localhost:8081/product/";
        //使用exchange执行post请求 传入参数 发送方式 访问路径
        // HttpMethod.PUT/DELETE/POST/GET
        RequestEntity<Product> requestEntity = new RequestEntity<>(product, HttpMethod.POST, URI.create(url));
        ResponseEntity<Void> responseEntity = restTemplate.exchange(requestEntity, Void.class);

    }

响应结果泛型处理ParameterizedTypeReference (指定返回值类型)

    public void hah(Product product){
        String url="http://localhost:8081/product/";

       // 响应结果泛型处理:ParameterizedTypeReference 
        RequestEntity<Void> requestEntity = new RequestEntity<>(HttpMethod.GET, URI.create(url));
        
        //可以指定返回值类型  请求体 返回值类型
        ResponseEntity<List<Product>> responseEntity = restTemplate.exchange(requestEntity,
                new ParameterizedTypeReference<List<Product>>() {
             });
    }

祝你幸福
送你一首歌《我好像在哪见过你》 薛之谦
附图1024程序员节 10-24-2018
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值