Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-RS Encoding

I'm using JAX-RS to create a web (rest) service that returns results in JSON format.

Everything is OK, except the encoding.

For example, I get:

 ..., parameter:"Dep\u00f3sitos" ,...

Instead of:

 ..., parameter:"Depósitos" ,...

I've tried using:

@Produces("application/json; charset=UTF-8")

but the problem remains. If I return it as XML using just:

@Produces("application/xml")

Everything is ok.

What do I need to set to produce the right type?

like image 700
RedEagle Avatar asked Mar 02 '11 16:03

RedEagle


People also ask

How do you encode a response in Java?

The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. The response's character encoding is only set from the given content type if this method is called before getWriter is called. This method may be called repeatedly to change content type and character encoding.

What is a JAX-RS application?

JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture. The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.

Why do we need JAX-RS?

Why use JAX-RS / Jersey? Because it makes the development of RESTful services easier. JAX-RS is a standard that makes it easy to create a RESTful service that can be deployed to any Java application server: GlassFish, WebLogic, WebSphere, JBoss, etc.


Video Answer


2 Answers

All you need is this:

String back = "Depósitos";
return new String(back.getBytes(), "UTF8");
like image 166
Frank Avatar answered Oct 11 '22 07:10

Frank


I ended up using GSON instead of IBM's JSON4J which proved to be much better at handling custom Java class serialization.

like image 24
RedEagle Avatar answered Oct 11 '22 07:10

RedEagle