Add my custom http header to Spring RestTemplate request / extend RestTemplate

后端 未结 3 658
名媛妹妹
名媛妹妹 2020-12-13 17:50

My current code:

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Mall[] m         


        
相关标签:
3条回答
  • 2020-12-13 18:18

    You can pass custom http headers with RestTemplate exchange method as below.

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("X-TP-DeviceID", "your value");
    
    HttpEntity<RestRequest> entityReq = new HttpEntity<RestRequest>(request, headers);
    
    RestTemplate template = new RestTemplate();
    
    ResponseEntity<RestResponse> respEntity = template
        .exchange("RestSvcUrl", HttpMethod.POST, entityReq, RestResponse.class);
    

    EDIT : Below is the updated code. This link has several ways of calling rest service with examples

    RestTemplate restTemplate = new RestTemplate();
    
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("X-TP-DeviceID", "your value");
    
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
    
    ResponseEntity<Mall[]> respEntity = restTemplate.exchange(url, HttpMethod.POST, entity, Mall[].class);
    
    Mall[] resp = respEntity.getBody();
    
    0 讨论(0)
  • 2020-12-13 18:20

    If the goal is to have a reusable RestTemplate which is in general useful for attaching the same header to a series of similar request a org.springframework.boot.web.client.RestTemplateCustomizer parameter can be used with a RestTemplateBuilder:

     String accessToken= "<the oauth 2 token>";
     RestTemplate restTemplate = new RestTemplateBuilder(rt-> rt.getInterceptors().add((request, body, execution) -> {
            request.getHeaders().add("Authorization", "Bearer "+accessToken);
            return execution.execute(request, body);
        })).build();
    
    0 讨论(0)
  • 2020-12-13 18:22

    Add a "User-Agent" header to your request.

    Some servers attempt to block spidering programs and scrapers from accessing their server because, in earlier days, requests did not send a user agent header.

    You can either try to set a custom user agent value or use some value that identifies a Browser like "Mozilla/5.0 Firefox/26.0"

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("user-agent", "Mozilla/5.0 Firefox/26.0");
    headers.set("user-key", "your-password-123"); // optional - in case you auth in headers
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
    ResponseEntity<Game[]> respEntity = restTemplate.exchange(url, HttpMethod.GET, entity, Game[].class);
    
    logger.info(respEntity.toString());
    
    0 讨论(0)
提交回复
热议问题