Programming & Development / April 19, 2025

How to Use RestTemplate with Custom Headers in Spring Boot (+ Application Properties as Map)

Spring Boot RestTemplate HTTP headers GET request POST request HttpEntity HttpHeaders exchange method @Value @ConfigurationProperties application.properties URL mapping Map injection

1. Configuring RestTemplate Bean

Create a configuration class to define your RestTemplate bean:

java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

2. Make GET Request with Headers

java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MyService {

    @Autowired
    private RestTemplate restTemplate;

    public String getExample() {
        String url = "https://api.example.com/data";

        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer your-token-here");
        headers.set("Accept", "application/json");

        HttpEntity<String> entity = new HttpEntity<>(headers);

        ResponseEntity<String> response = restTemplate.exchange(
            url, HttpMethod.GET, entity, String.class);

        return response.getBody();
    }
}

3. Make POST Request with Headers and Body

java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MyService {

    @Autowired
    private RestTemplate restTemplate;

    public String postExample() {
        String url = "https://api.example.com/data";

        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer your-token-here");
        headers.set("Content-Type", "application/json");

        String requestBody = "{\"key\":\"value\"}";

        HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);

        ResponseEntity<String> response = restTemplate.exchange(
            url, HttpMethod.POST, entity, String.class);

        return response.getBody();
    }
}

4. Application Properties with @Value

🔹 application.properties

properties

Test.Val.url.url1=urlbababa
Test.Val.url.url2=urlsmmmmm

🔹 Use @Value to Inject Individual Properties

java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class UrlConfig {

    @Value("${Test.Val.url.url1}")
    private String url1;

    @Value("${Test.Val.url.url2}")
    private String url2;

    public String getUrl1() {
        return url1;
    }

    public String getUrl2() {
        return url2;
    }
}

5. Injecting as Map<String, String> using @ConfigurationProperties

🔹 application.properties

properties

Test.Val.url.url1=urlbababa
Test.Val.url.url2=urlsmmmmm

🔹 Configuration Class

java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "Test.Val")
public class UrlConfig {

    private Map<String, String> url;

    public Map<String, String> getUrl() {
        return url;
    }

    public void setUrl(Map<String, String> url) {
        this.url = url;
    }
}

🔹 Access Map in a Service

java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MyService {

    private final UrlConfig urlConfig;

    @Autowired
    public MyService(UrlConfig urlConfig) {
        this.urlConfig = urlConfig;
    }

    public void printUrls() {
        Map<String, String> urlMap = urlConfig.getUrl();
        System.out.println("URL 1: " + urlMap.get("url1"));
        System.out.println("URL 2: " + urlMap.get("url2"));
    }
}



Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex