users@jersey.java.net

[Jersey] Re: how to rename XmlRootElement in JSON

From: Maxrunner <joao.rossa_at_gmail.com>
Date: Thu, 16 Jun 2011 10:28:20 -0700 (PDT)

Hi all! im trying to convert my calendar properties to another format, by
default jackson seems to be converting to the timeinmillis value, but i want
it to conveted as "dd/MM/YYYY" for example, im trying to use the
@xmlJavaTypeAdapter annotation that jackson seems to support but im not
getting the right results, it still display the calendar im milliseconds i
think.
*Here my pojo:*

import java.util.Calendar;
import java.util.List;

import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import sample.hello.resources.jacksonparserutils.CalendarAdapter;

@XmlRootElement(name="Funcionario")
public class FuncionarioEntidadeMobile {

private int idFuncionario;
private String loginUser;
private String nome;
private String morada;
private String concelho;
private String freguesia;
private String localidade;
private String codigoPostal;
private String telefone;
private String telemovel;
private String fax;
private String email;
@XmlJavaTypeAdapter(CalendarAdapter.class)
public Calendar dataNascimento;
private int sexo;
private String estadoCivil;
private int numeroDependentes;
private String numeroBI;
@XmlJavaTypeAdapter(CalendarAdapter.class)
private Calendar dataEmissao;
private String arquivo;
private String numeroFiscal;
private String numeroSS;
private String habilitacoes;
@XmlJavaTypeAdapter(CalendarAdapter.class)
private Calendar dataAdmissao;
private OrganicaMobileEntity orgPrincipal;
private List<OrganicaFuncaoMobileEntity> orgsFuncs;

public int getIdFuncionario() {
return idFuncionario;
}

.....
....
}

*Here's the type adapter:*

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class CalendarAdapter extends XmlAdapter<String, Calendar>
{

 DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

@Override
public Calendar unmarshal(String date) throws Exception
{
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(df.parse(date));
 return calendar;
}
@Override
public String marshal(Calendar calendar) throws Exception
{
 return df.format(calendar.getTime());
}

}

*But it still shows as before, when i try to debug, the calendaradapter
doesnt seem to be reached, i also have the jaxb introspection annotation
configured in the context resolver:*

@Provider
public class JacksonObjectMapperProvider implements
ContextResolver<ObjectMapper> {
 //object mappers to be used for configurations and register with jersey
private ObjectMapper defaultObjectMapper;
private ObjectMapper listsObjectMapper;
     public JacksonObjectMapperProvider() {
super();
 MyObjectMapperProvider();
}

public void MyObjectMapperProvider() {
 * **AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
*
* AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();*
* AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary,
secondary);*

//object provider for default objects - show root name
    defaultObjectMapper = new ObjectMapper();
    defaultObjectMapper.configure(Feature.INDENT_OUTPUT, true);
    defaultObjectMapper.configure(Feature.WRAP_ROOT_VALUE, true);

    // make deserializer use JAXB annotations

defaultObjectMapper.getDeserializationConfig().setAnnotationIntrospector(pair);
 // make serializer use JAXB annotations

defaultObjectMapper.getSerializationConfig().setAnnotationIntrospector(pair);


    //object provider for list type objects - do not show root name
    listsObjectMapper= new ObjectMapper();
    listsObjectMapper.configure(Feature.INDENT_OUTPUT, true);
    listsObjectMapper.configure(Feature.WRAP_ROOT_VALUE, false);

    // make deserializer use JAXB annotations

listsObjectMapper.getDeserializationConfig().setAnnotationIntrospector(pair);
 // make serializer use JAXB annotations

listsObjectMapper.getSerializationConfig().setAnnotationIntrospector(pair);
    }

 //return context according to type
public ObjectMapper getContext(Class<?> type) {
   if (type == ListWrapperWithMap.class) {
             return listsObjectMapper;
         } else {
             return defaultObjectMapper;
         }

}
}

The other jaxb annotations i have seem to be working....
Any insight?

regards,

2011/6/14 João Rossa <joao.rossa_at_gmail.com>

>
> Hi there, i solved the problem using the spring encoder that forces it to
> UTF-8, and i've put it in the web.xml:
>
> <filter>
> <filter-name>CharacterEncodingFilter</filter-name>
>
> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
> <init-param>
> <param-name>encoding</param-name>
> <param-value>UTF-8</param-value>
> </init-param>
> <init-param>
> <param-name>forceEncoding</param-name>
> <param-value>true</param-value>
> </init-param>
> </filter>
> <filter-mapping>
> <filter-name>CharacterEncodingFilter</filter-name>
> <url-pattern>/rest/*</url-pattern>
> </filter-mapping >
>
>
> seems like its working now :)
>
> thanks for all the help.
>
>
> On Tue, Jun 14, 2011 at 7:00 PM, Cowtowncoder [via Jersey] <
> ml-node+6475255-699545422-73314_at_n2.nabble.com> wrote:
>
>> On Tue, Jun 14, 2011 at 3:23 AM, Maxrunner <[hidden email]<http://user/SendEmail.jtp?type=node&node=6475255&i=0>>
>> wrote:
>>
>> > Yeah, with this resource:
>> > @GET
>> > @Produces("application/json")
>> > @Path("/getOrganica")
>> > public Response getOrganica(){
>> > System.out.println("super porra utf-8!");
>> > Object entity=fillOrganica(123,"Organicão Mind","2010-12-23",true);
>> > //return fillOrganica(123,"Organição Mind","2010-12-23",true);
>> > return Response.ok(entity).header("charset", "UTF-8").build();
>> > }
>> >
>> > I get this on chrome:
>> >
>> > {
>> > "Organica" : {
>> > "designacao" : "Organicão Mind",
>>
>> It looks like Chrome somehow assumes that encoding of content is
>> latin-1; and as such incorrectly decodes byte stream (which is in
>> UTF-8) to display two characters, instead of a two-byte Unicode
>> character it should display.
>>
>> Does anyone else know if this is a known Chrome issue? I think it is
>> also possible to define encoding with media type used with @Produces;
>> something like "application/json; charset=utf-8". Would that help in
>> your case?
>>
>> -+ Tatu +-
>>
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6475255.html
>> To unsubscribe from how to rename XmlRootElement in JSON, click here<http://jersey.576304.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=6173292&code=am9hby5yb3NzYUBnbWFpbC5jb218NjE3MzI5MnwyMDYzODA1MDkw>.
>>
>>
>
>


--
View this message in context: http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6484007.html
Sent from the Jersey mailing list archive at Nabble.com.