users@glassfish.java.net

Jax-RS UTF-8 consume

From: Daoud AbdelMonem Faleh <abdelmonem.faleh_at_gmail.com>
Date: Thu, 20 Mar 2014 16:47:32 +0100

Hi,

Am running Jax-RS end-points on Glassfish 4.0 with Backbone MarionetteJS
HTML client (uses JQuery internally) so far everything works correctly
but when I POST a JSON payload with arabic content it hits the database
(Postgres) with wrong encoding. If I directly modify the database row
content with arabic content it gets displayed correctly.

Postgres 9.2 database is created with:
WITH ENCODING = 'UTF8'

In glassfish-resources I have:
<property name="URL"
value="jdbc:postgresql://localhost:5432/kistas?charSet=UTF-8"/>

In glassfish-web.xml I have:
<glassfish-web-app>
     <parameter-encoding default-charset="UTF-8"/>
</glassfish-web-app>

In the JAX-RS Resource I have:
...
@GET
     @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
     public Response getCabinets(@QueryParam("from") @DefaultValue("0")
int from,
             @QueryParam("to") @DefaultValue("10") int to) {
         List<Cabinet> cabinets = cabinetService.getCabinets(from, to);
         GenericEntity<List<Cabinet>> entity = new
GenericEntity<List<Cabinet>>(cabinets){};
         return Response.ok(entity).build();
     }

...
@POST
     @Consumes(MediaType.APPLICATION_JSON + ";charset=UTF-8")
     public Response createCabinet(@Context UriInfo uriInfo, Cabinet cab) {
         System.out.println(cab);
         Cabinet createdCab = cabinetService.createCabinet(cab);
         URI uri =
uriInfo.getAbsolutePathBuilder().path(createdCab.getId()).build();
         return Response.created(uri).build();
     }

Thanks,
--Daoud