RestTemplate使用

admin
2022-04-14 / 0 评论 / 42 阅读 / 正在检测是否收录...

RestTemplate使用

一、依赖注入

    @Bean
    // 开启负载均衡
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

二、调用服务

String url ="http://provider/getHi";
String respStr = restTemplate.getForObject(url, String.class);

三、get 请求

1、getForEntity

getForEntity方法的返回值是一个ResponseEntity,ResponseEntity是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。

<200,Hi,[Content-Type:"text/plain;charset=UTF-8", Content-Length:"8", Date:"Fri, 10 Apr 2020 09:58:44 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>

2、返回Map

调用方

        String url ="http://provider/getMap";
        ResponseEntity<Map> entity = restTemplate.getForEntity(url, Map.class);
        System.out.println("respStr: "  + entity.getBody() );

生产方

    @GetMapping("/getMap")
    public Map<String, String> getMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "500");
        return map; 
    }

3、返回对象(getForEntity)

调用方

        ResponseEntity<Person> entity = restTemplate.getForEntity(url, Person.class);
        System.out.println("respStr: "  + ToStringBuilder.reflectionToString(entity.getBody() ));

生产方

    @GetMapping("/getObj")
    public Person getObj() {
        Person person = new Person();
        person.setId(100);
        person.setName("xiaoming");
        return person; 
    }

Person类

    private int id;
    private String name;

4、传参调用

4.1、使用占位符

    String url ="http://provider/getObjParam?name={1}";
    ResponseEntity<Person> entity = restTemplate.getForEntity(url, Person.class,"hehehe...");

4.2、使用map

        String url ="http://provider/getObjParam?name={name}";
        Map<String, String> map = Collections.singletonMap("name", " memeda");
        ResponseEntity<Person> entity = restTemplate.getForEntity(url, Person.class,map);

5、返回对象(getForObject)

Person person = restTemplate.getForObject(url, Person.class,map);

四、post 请求

1、postForEntity

调用方

        String url ="http://provider/postParam";
        Map<String, String> map = Collections.singletonMap("name", " memeda");
        ResponseEntity<Person> entity = restTemplate.postForEntity(url, map, Person.class);

生产方

    @PostMapping("/postParam")
    public Person postParam(@RequestBody String name) {

        System.out.println("name:" + name);

        Person person = new Person();
        person.setId(100);
        person.setName("xiaoming" + name);
        return person; 
    }

2、postForLocation

调用方

        String url ="http://provider/postParam";
           
        Map<String, String> map = Collections.singletonMap("name", " memeda");
        URI location = restTemplate.postForLocation(url, map, Person.class);
        
        System.out.println(location);

生产方

需要设置头信息,不然返回的是null

    public URI postParam(@RequestBody Person person,HttpServletResponse response) throws Exception {

    URI uri = new URI("https://www.baidu.com/s?wd="+person.getName());
    response.addHeader("Location", uri.toString());

五、exchange

可以自定义http请求的头信息,同时保护get和post方法

第一步:自定义拦截器

需要实现ClientHttpRequestInterceptor接口

public class LoggingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {

        System.out.println("拦截啦!!!");
        System.out.println(request.getURI());

        ClientHttpResponse response = execution.execute(request, body);

        System.out.println(response.getHeaders());
        return response;
    }

第二步:添加到RestTemplate中

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add(new LoggingClientHttpRequestInterceptor());
        return restTemplate;
    }
3

评论 (0)

取消