合并所有的http头到单行。

huangapple go评论97阅读模式
英文:

Merge all httpHeader to single line

问题

  1. 我正在使用以下代码片段打印所有标头
  2. import org.springframework.http.HttpHeaders;
  3. @RequestHeader HttpHeaders httpHeaders
  4. httpHeaders.forEach((key,value) ->{
  5. LOGGER.info("Header Name: "+key+" Header Value: "+value);
  6. });
  7. 有没有一种好的方法将所有内容放在一行?(我能想到的是使用StringBuilder)(我的意图是减少日志记录
英文:

I am using the below snippet to print all headers.

  1. import org.springframework.http.HttpHeaders;
  2. @RequestHeader HttpHeaders httpHeaders
  3. httpHeaders.forEach((key,value) ->{
  4. LOGGER.info("Header Name: "+key+" Header Value: "+value);
  5. });

is there a good way to put all to single line? (StringBuilder I can think of) (My intention is to reduce logging)

答案1

得分: 1

你可以在forEach的主体中使用StringBuilderappend方法,或者可以使用以下方式:

  1. String logString = httpHeaders.entrySet().stream()
  2. .map(e -> "Header Name: " + e.getKey() + " Header Value: " + e.getValue())
  3. .collect(Collectors.joining(", ")); // 在这里选择任何分隔符
  4. LOGGER.info(logString);

希望这对你有所帮助。

英文:

You can use call StringBuilder's append in the body of the forEach, alternatively use

  1. String logString = httpHeaders.entrySet().stream()
  2. .map(e -> "Header Name: "+e.getKey()+" Header Value: "+e.getValue())
  3. .collect(Collectors.joining(", ")); //choose any separator here
  4. LOGGER.info(logString);

huangapple
  • 本文由 发表于 2023年3月9日 23:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定