Search code examples
javaspring-bootresttemplate

How to add custom header with <String, Integer> to Spring Boot HttpHeaders?


Currently Spring Boot HttpHeaders header only takes <String, String> pairs. How can I add a header with <String, Long>?

I need to integrate with one of the external product API in my Java Program, to access this API, it needs a token header which for this product, all of its APIs only takes Numbers(long). It is so out of ordinary and it gives me a lot headache. I am using RestTemplate and I've searched a lot place with no luck.

Did any of you have done anything similar to this? Need some help.


Solution

  • You can create generic header using MultiValueMap that accepts String as key and Object as value

    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.add("Header1", 11111);
    

    And then create generic HttpEntity of object type by passing MultiValueMap as headers

    HttpEntity<Object> entity = new HttpEntity<>(map);
    
    System.out.println(entity);