0

I'm working on developing some REST web services for an existing Java application. I'm using Resteasy JAX-RS by JBoss (as the existing application runs on a JBoss serer).

The web serivce below is supposed to return a User object based on the id number.

@GET
@Path("/getUser")
@Produces(MediaType.APPLICATION_XML)
public User getUser(@QueryParam("userId") int userId) {
    return User.load(userId);
}

The User contains at least an ID and a name.

public class User {
    private int id;
    private String name;
}

When JAX-RS produces the XML document, it fails to handle special characters like æ ø å, that may occur in a name.

The output XML claims to be encoded in UTF-8, but it still fails to show æ ø å.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <user>
        <id>18549</id>
        <name>Københavner 1</name>
    </user>

Every object I try to convert to XML this way fails to encode 'æøå' properly.

Any suggestions? Searching the web didn't help much either.

UPDATE: When I receive the XML on the client side, I first handle it as a String before using JAXB to unmarshal it into an object. I forgot to set the encoding of the String to UTF-8. So basically doing this before unmarshalling:

new String(((String) response).getBytes(), "UTF-8");
ChristofferPass
  • 346
  • 1
  • 11

2 Answers2

0

Have you tried adding a charset:

@GET
@Path("/getUser")
@Produces("application/xml; charset=UTF-8")
public User getUser(@QueryParam("userId") int userId) {
    return User.load(userId);
}

UPDATE:

Since it didn't help setting the charset, your problem is that you are consuming the ouput as ISO-8859-1.

My guess is that you want the unicode character U+00F8, or "latin small letter o with stroke": ø. The UTF-8 encoding for that is 0xC3B8. In ISO-88591-1 C3 is à and B8 is ¸ (cedilla).

So make sure that the application receiving the output understand UTF-8 and displays it correctly.

forty-two
  • 12,204
  • 2
  • 26
  • 36
  • Thanks for pointing me in this direction. When I receive the XML on the client side, I first handle it as a String before using JAXB to unmarshal it into an object. I forgot to set the encoding of the String to UTF-8. Thanks! – ChristofferPass Apr 20 '15 at 07:54
0

You may try this How to set the charset with JAX-RS?

@Produces("application/xml; charset=UTF-8") 
Community
  • 1
  • 1
dan_
  • 1
  • 2