2
votes

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.

1

1 Answers

3
votes

Sorry for that, but I have to resolve the question. The solution is to go for the latest stable MOXy libs 2.5.2. This solved my problems like a charme (lucky me). Ok it's just a couple of minutes ago I asked this question but be sure that I posted it after desparatley looking for a solution since the last couple of days. The only hint I had was https://bugs.eclipse.org/bugs/show_bug.cgi?id=419072#attach_236300 but this just mentioned an odd behaviour with control characters.

Hope this helps anyone having the same trouble.