Home:ALL Converter>Java Restful service encoding with special chars

Java Restful service encoding with special chars

Ask Time:2012-09-21T22:23:12         Author:arlievanos

Json Formatter

im having problem whith a POST request. I have to receive an XML message and im parsing it with JAXB. Everithing works fine but when special chars are included in the message like

<?xml version="1.0" encoding="UTF-8"?>
<sending>
    <sms>
        <message>España</message>
    </sms>
</sending>

it throws a Bad Request Error (400) The request sent by the client was syntactically incorrect (Bad Request).

This is my code to process the xml message

@POST
@Path("smsSendPost")
@Produces(MediaType.APPLICATION_XML)
public Response smsSendPost(JAXBElement<Sending> xml){
    Sending sending = xml.getValue();

    //Process the message
}

Im using tomcat to test it.

Can anyone help me with this?

UPDATE: I've just found the cause of the problem is the tomcat, when I send the request via POST with ISO-8859-1 encoding the service works fine. The solution is to change the encoding of the tomcat.

Author:arlievanos,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12532441/java-restful-service-encoding-with-special-chars
Bionic_Geek :

The content type could be the issue but that depends on what content type you generate in the POST on the client and what character set is being used by the XML parser on the receiving end. \n\nOne alternative which is feasible in many situations like this is to use CDATA (unparsed character data) blocks in your XML where special characters can occur.\n\n<sending>\n <sms>\n <message><![CDATA[España]]></message>\n </sms>\n</sending>\n\n\nBy default an XML parser will attempt to parse all of the text in an XML message. CDATA blocks tell a XML parser not to parse the characters contained within the CDATA block. \n\nIf your service that is receiving this cannot identify the character, it may be erroring out or assuming the XML is not well formed and throwing the 400 you are seeing.\n\nMore on CDATA blocks : http://www.w3schools.com/xml/xml_cdata.asp",
2012-09-21T15:29:46
arlievanos :

I've just found that the cause of the problem is the tomcat, when I send the request via POST with ISO-8859-1 encoding the service works fine. I guess the solution is to change the encoding of the tomcat.",
2012-10-05T19:14:14
yegor256 :

Looks like your client is sending an invalid request. I suspect that you're using some non-UTF8 encoding and doesn't specify its name in Content-Type header. If you post full HTTP request I may be able to diagnose the problem with more details.",
2012-09-21T15:06:23
yy