I got really stuck with Eclipselink MOXy 2.5.1 not delivering me correctly encoded Asian characters (or other ones, e.g. German umlauts äöü).
My code:
@GET
@Produces({MediaType.APPLICATION_JSON + ";charset=UTF-8"})
@Path("/test")
public Response getJson() throws IOException{
return Response.ok(new Test()).build();
}
@GET
@Produces({MediaType.APPLICATION_JSON + ";charset=UTF-8"})
@Path("/test2")
public Response getKey() throws IOException{
return Response.ok(new Test().toString()).build();
}
The Test class looks like:
@XmlRootElement
class Test{
public String key;
public Test() throws IOException {
key = FileUtils.readFileToString(new File("e:\\utf8.txt"), "UTF-8");
}
public String toString() {
return key;
}
}
The property "key" is initialized for testing purpose only with one string which i read from an UTF-8 encoded file without BOM containing
アナログカメラは
When I make a call to both resources with a client:
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("http://127.0.0.1/rest").path("/test");
WebTarget webTarget2 = client.target("http://127.0.0.1/rest").path("/test2");
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
invocationBuilder.header(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
Response response = invocationBuilder.get();
System.out.println("Result /test: " + response.readEntity(String.class));
invocationBuilder = webTarget2.request(MediaType.APPLICATION_JSON_TYPE);
invocationBuilder.header(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
response = invocationBuilder.get();
System.out.println("Result /test2: " + response.readEntity(String.class));
I get the following results:
Result /test: {"key":"��?��o"}
Result /test2: アナログカメラは
The strange thing is, that when I'm marshalling the test object's toString() method in /test2 will return me correctly encoded characters, bu marshalling the test object with /test won't.
Any idea? I'm kind of lost.