Problems with Jersey, REST, JSON and UTF-8 [Update]

UTF-8 is always a problem. Unbelievable. 2008 and we still haven’t fixed this. One of my current projects is a Javascript frontend with a REST backend. The backend stores to MySQL (a famous UTF-8 trouble maker) and creates JSON to REST calls. The problems starts with UTF-8 characters. Somewhere in the callchain – as always – characters don’t get correctly written. MySQL and the JDBC driver should work, the JSP page is UTF-8 (@page and meta-equiv), jQuery – which does the AJAX – and JS do know UTF-8 and Jersey should be UTF-8 too. But with some experiments now I’m quite sure that Jersey (JSR 311 REST framework) is to blame. I’m not sure how to specify UTF-8, this

doesn’t help. Funny, every major project with several frameworks along the call chain and several languages (JS, C, Java) makes UTF-8 problems somehow. I’m so fed up with this, it’s 2008.

Update: Jersey uses InputStreams for all encodings, especially StringProvider is relevant to me (se above). Does this work with Unicode?

Adding Web Beans JSR 299 to Jersey for REST

While playing with Web Beans I thought it would be nice to add Web Beans support to Jersey. Jersey is a JSR 311 implementation for RESTful web services in Java. Though it has taken some flak, I – and others – think it’s easy to use. Because it’s easy in Jersey to control the creation of objects by writing your own servlet with a Jersey ComponentProvider, I finished a quick hack in no time. Some help was the integration examples for Spring.

Having defined this servlet, a REST resource example for the UuidService from my last post about Web Beans, looks like this

Works like a charm. The UUIDService is injected through Web Beans and the scopes from Web Beans, @RequestScope and @SessionScope seem to work. This takes a big burden from the developer.

I hope to implement a SOFEA web application with Javascript parts which communicate with the backend via REST. For performance reasons it would be nice to populate and render the Javascript on the server. Therefore I would wish I could resolve REST calls to Jersey internally like

to pre-populate pages with REST calls in them.

Thanks for listening.

Update:The Jersey lead wrote about a Jersey client API which perhaps does what I want. Splendid.