cancel
Showing results for 
Search instead for 
Did you mean: 

Jackson Annotations not working

mangar
Star Contributor
Star Contributor
I have an object that I am trying to serialize in a webscript.  I have bee doing this forever now:

            res.addHeader("Accept", "application/json");
            res.addHeader("Content-type", "application/json; charset=UTF-8");
       res.getWriter().write(mapper.writeValueAsString(myObject));


and everything is fine.

I have now added annotations to my beans. I have an abstract superclass that I use @JsonTypeInfo, and a @JsonIgnore for some field.
However, I do NOT see the @class property that should have been added, and the field marked ignore is showing up.

I then added this as a test.


@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
public abstract class A {

}

public class B extends A {
   
   private String x;

   public String getX() {
      return x;
   }

   public void setX(String x) {
      this.x = x;
   }
   
}


and in a webscript I do this:


      B b = new B();
      b.setX("Whatever");      
      mapper = new ObjectMapper();
      System.out.println("JSON:"+mapper.writeValueAsString(b));


and my output is:

JSON:{"x":"Whatever"}


when I run it in a static main() I get the correct output:

JSON:{"@class":"org.xxx.alfresco.web.B","x":"Whatever"}


Is there some special thing I need to do to turn on annotaions for Alfresco webscripts?
1 REPLY 1

mangar
Star Contributor
Star Contributor
It is also helpfull to use the import com.fasterxml.jackson.databind.ObjectMapper; and not the default import org.codehaus.jackson.map.ObjectMapper; that alfresco uses.

My apologies to the alfresco forums for being a complete idiot.