[英]How to add custom header with <String, Integer> to Spring Boot HttpHeaders?
Currently Spring Boot HttpHeaders header only takes <String, String>
pairs.目前 Spring Boot HttpHeaders 标头只需要<String, String>
对。 How can I add a header with <String, Long>
?如何添加带有<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).我需要与我的 Java 程序中的一个外部产品 API 集成,要访问这个 API,它需要一个令牌标头,对于这个产品,它的所有 API 只需要数字(长)。 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.我正在使用 RestTemplate 并且我搜索了很多地方都没有运气。
Did any of you have done anything similar to this?你们中有人做过类似的事情吗? Need some help.需要一些帮助。
You can create generic header using MultiValueMap that accepts String
as key and Object
as value您可以使用接受String
作为键和Object
作为值的MultiValueMap创建通用标头
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("Header1", 11111);
And then create generic HttpEntity of object type by passing MultiValueMap
as headers然后通过将MultiValueMap
作为标头传递来创建对象类型的通用HttpEntity
HttpEntity<Object> entity = new HttpEntity<>(map);
System.out.println(entity);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.