ribbon的getForObject和getForEntity的区别

getForObject:
返回对象为响应体中数据转化成的对象,基本上可以理解为Json
getForEntity:
返回对象为ResponseEntity对象,包含了响应中的一些重要信息,比如响应头、响应状态码、响应体等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@GetMapping("/consumer/payment/get/{id}")
    public CommonResult get(@PathVariable("id") Long id){<!-- -->
        return template.getForObject(url+"/payment/get/"+id,CommonResult.class);
     

@GetMapping("/consumer/payment/getForEntity/{id}")
    public CommonResult getForEntity(@PathVariable("id") Long id){<!-- -->
        ResponseEntity<CommonResult> entity = template.getForEntity(url + "/payment/get/" + id, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful())
        {<!-- -->
            return entity.getBody();
        }
        else {<!-- -->
            return new CommonResult(444,"返回错误");
        }
    }

两个方法对比,
在这里插入图片描述
在这里插入图片描述
返回值是一样的,因为他们都是从我8002端口出去的,不同的是entity是可以获取我们的响应状态码和响应体的。