I think I may have found the answer. See 
http://java.sun.com/docs/books/tutorial/i18n/text/string.html ). 

Here, they talk about how it is incorrect to load a string value into a bytes 
array without specifying the encoding. Notice, in their example, that the 
difference in 

byte[] bytes = utfEightString.getBytes(); \\ bad
byte[] bytes = utfEightString.getBytes("UTF8"); \\ good

will produce a different number of byte elements. Essentially, your loop is 
doing the same thing. For a five characters string, you will always end up with 
a 5 element byte array, when it might have given you an 8 byte array, had you 
specified the encoding.

Instead, you should do this:

byte[] bytes = utfEightString.getBytes("UTF8");
return new String(bytes, "UTF8");


Ron Grimes




-----Original Message-----
From: Loh Kok Jeng [mailto:[email protected]] 
Sent: Wednesday, June 23, 2010 7:11 PM
To: [email protected]
Subject: Re: UTF-8 Characters in Request Params for REST Service

Hi Sergey,

This is for form based submission.

Below is a snippet of my code.  I want to be able to accept non-ASCII
characters in "message" parameter below.  However, when I tested with
Chinese characters, they are converted to "?" as shown in my logs.

Any help will be very much appreciated.

   @POST
    // @Consumes("application/x-www-form-urlencoded")
    public SMSSendSmsResponse sendSms(@HeaderParam("Authorization")
String authorization,
                                      @Context MessageContext mc,
@FormParam("address") List<String> address,
                                      @FormParam("message") String message,
                                      @FormParam("notifyURL") String notifyURL,
                                      @FormParam("correlator") String
correlator,
                                      @FormParam("senderName") String
senderName,
                                      @FormParam("Charging") String Charging)
        throws MalformedURLException,  PolicyException, ServiceException {

        if (address != null) {
            for (Iterator<String> i = address.iterator(); i.hasNext();) {
                log.info("Address: " + i.next());
            }
        }
        log.info("Message: " + message);
        log.info("Notify URL: " + notifyURL);
        log.info("Correlator: " + correlator);
        log.info("Sender Name: " + senderName);
        log.info("Charging: " + Charging);

        ....

}
regards,
Loh Kok Jeng



On 24 June 2010 02:00, Sergey Beryozkin <[email protected]> wrote:
> I need to know the details of the request and how a resource method
> expecting the values looks like
>
> thanks, Sergey
>
> On Tue, Jun 22, 2010 at 7:10 PM, Rakesh Rai <[email protected]> wrote:
>
>> This is the method that does the trick.... converts to UTF-8 and transforms
>> to UTF-8 format string and sends it back to the caller
>> Wherever in your service class / action class add this method and convert
>> the existing string to return the UTF-8 transformed string / text
>>    /**
>>     *
>>     * @param utfEightString
>>     * @return String converted to USF-8 format and send it to the caller .
>>     * @throws java.io.
>> UnsupportedEncodingException
>>     */
>>    public static final String utfEightConvert(String utfEightString)
>>            throws java.io.UnsupportedEncodingException {
>>
>>        byte[] bytes = new byte[utfEightString.length()];
>>        for (int i = 0; i < utfEightString.length(); i++) {
>>            bytes[i] = (byte) utfEightString.charAt(i);
>>        }
>>        return new String(bytes, "UTF-8");
>>
>>    }
>>
>> Hope it helps.
>> Rakesh
>>
>>
>> On Tue, Jun 22, 2010 at 1:47 PM, Sergey Beryozkin <[email protected]
>> >wrote:
>>
>> > Is it a form based submission ? or XML is posted in the body ?
>> >
>> >
>> > cheers, Sergey
>> >
>> > On Mon, Jun 21, 2010 at 6:00 PM, Loh Kok Jeng <[email protected]>
>> > wrote:
>> >
>> > > Dear all,
>> > >
>> > > I'm struggling with non-ASCII characters passed in request params of a
>> > > REST service.  The characters become ? when received by my app
>> > > developed using CXF.  The encoding is set to UTF-8 by the REST client.
>> > >  Why do I get "?"?
>> > >
>> > > Thanks in advance.
>> > >
>> >
>>
>

Reply via email to