2
0

[Java]HttpHeadersからMultiValueMapに変換

Last updated at Posted at 2024-03-17
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;

public class HttpHeadersToMultiValueMapConverter {

    public static MultiValueMap<String, String> convert(HttpHeaders headers) {
        MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap();
        
        headers.forEach((key, values) -> {
            multiValueMap.put(key, values);
        });
        
        return multiValueMap;
    }

    public static void main(String[] args) {
        HttpHeaders headers = new HttpHeaders();

        // java.nio.charset.StandardCharsets は、US-ASCII、ISO-8859-1、UTF-8、UTF-16BE、UTF-16LE、UTF-16のみでWindow-31jとかMS932は無い
        // 文字コードの指定はCharset.forName()
        //headers.setContentType(new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8));
        //headers.setContentType(new MediaType(MediaType.APPLICATION_JSON, Charset.forName("utf-8")));
        headers.setContentType(new MediaType(MediaType.APPLICATION_JSON, Charset.forName("MS932")));
        
        MultiValueMap<String, String> convertedHeaders = convert(headers);
        
        System.out.println("Converted MultiValueMap:");
        System.out.println(convertedHeaders);
    }
}

出力
MS932と指定。charsetはwindows-31jとなる。

Converted MultiValueMap:
[Content-Type:"application/json;charset=windows-31j"]
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0