Jin Kwon Jin Kwon - 3 months ago 9
HTTP Question

How can I parse charset value from the JAX-RS response?

On any

Response
whose
Content-Type
might have
charset
property,
How can I parse the value?

HTTP/1.1 200 OK
Content-Type: plain/text; charset=<UNKNOWN>


When clients need to parse as
Reader
, how can they detect the
charset
value?

Response response = request();
try (InputStream stream = response.readEntity(InputStream.class);
InputStreamReader reader = new InputStreamReader(stream, ????);
BufferedReader buffered = new BufferedReader(reader)) {
}

Answer

Anyone can use the MediaType#getParameters.

final MediaType mediaType = response.getMediaType();
// mediaType may be null
final Map<String, String> parameters = mediaType.getParameters();
final String charset = parameters.get("charset");
// charset, again, may be null
Comments