Client client response string encoding

I am using a jersey client to request a web service.

Client client = ClientBuilder.newClient(new ClientConfig());
Invocation.Builder builder = client.target("http://someurl.com").request();
String result = builder.get(String.class);

Then i got an answer

<?xml version="1.0" encoding="ISO-8859-1" ?>
<DATA>some data with é è à characters</DATA>

But in my result, the String answer looks like this:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<DATA>some data with       characters</DATA>

How can I tell jersey to correctly decode webservice response?

+4
source share
2 answers

I found a workaround at the moment

Client client = ClientBuilder.newClient(new ClientConfig());
Invocation.Builder builder = client.target("http://someurl.com").request();
Response response = builder.get();
String result = CharStreams.toString(new InputStreamReader((InputStream) response.getEntity(), Charsets.ISO_8859_1));

CharStreamsis a Guava class, but there is another way to convert InputStreamto Stringwith right Charset.

0
source

Thanks Wizbot, today I had the same problem.

I wanted to publish my java 8 solution without a dependency on Guava:

Client client = ClientBuilder.newClient(new ClientConfig());
Invocation.Builder builder = client.target("http://someurl.com").request();
Response response = builder.get();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((InputStream) response.getEntity(), StandardCharsets.ISO_8859_1));
String result = bufferedReader.lines().collect(Collectors.joining(""));
+3
source

Source: https://habr.com/ru/post/1628249/


All Articles