0

I am working on a Java EE project, and I had deployed my Rest Webservices using Jackson . the services works fine but one problem I have is that I don't get the root name :

here's an example of :


[{"idRegion":1,"intituleRegion":"Sous Massa Draa"},
{"idRegion":2,"intituleRegion":"Chawya W rdigha"},{"idRegion":3,"intituleRegion":"rabat sale zemmour zaer"},
{"idRegion":4,"intituleRegion":"grand casablanca "},
{"idRegion":5,"intituleRegion":"marrakech tansift lhawz"},{"idRegion":6,"intituleRegion":"essaouira"},
{"idRegion":7,"intituleRegion":"fes boulemane "}]

knowing that I had define the : @XmlRootElement(name="region")

JAXB BEAN :

package ma.propar.FireApp.Entites;


@Entity

@Table (name = "REGION")

@XmlAccessorType(XmlAccessType.NONE)

@XmlRootElement(name="region")

public class Region {

    @Id @GeneratedValue
    @Column( name = "ID_REGION" )
    @XmlElement
   private int idRegion;

    @Column( name = "INTITULE_REGION" )
    @XmlElement
   private String intituleRegion;

    @OneToMany ( targetEntity = Zone.class, cascade=CascadeType.ALL ,mappedBy="region")
    private List<Zone> zones;


    @WebMethod(exclude=true)
    public List<Zone> getZones() {
        return zones;
    }

    @WebMethod(exclude=true)
    public void setZones(List<Zone> zones) {
        this.zones = zones;
    }

    public Region() {

    }

    public int getIdRegion() {
        return idRegion;
    }

    public void setIdRegion(int idRegion) {
        this.idRegion = idRegion;
    }

    public String getIntituleRegion() {
        return intituleRegion;
    }

    public void setIntituleRegion(String intituleRegion) {
        this.intituleRegion = intituleRegion;
    }

    /*@WebMethod(exclude=true)
    public List<Zone> getZones() {
        return zones;
    }

    @WebMethod(exclude=true)
    public void setZones(List<Zone> zones) {
        this.zones = zones;
    }*/

}

Jackson with jax-rs :

<jaxrs:server id="regionservices" address="/regionservices">
    <jaxrs:serviceBeans>
        <ref bean="regionMetier" />
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />
    </jaxrs:providers>
    <jaxrs:extensionMappings>
        <entry key="json" value="application/json" />
    </jaxrs:extensionMappings>
</jaxrs:server>

any body can help ?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140

1 Answers1

1

By default, only core Jackson annotations are used for configuring data binding aspects. To enable JAXB annotation support, you need to:

  1. Include jackson-xc jar, which contains org.codehaus.jackson.xc.JaxbAnnotationIntrospector (Jackson 1.x) or, com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector (Jackson 2.x)

  2. Register this annotation introspector

You can find more details here

Jackson alternative to @XmlRootElement annotation is @JsonRootName.

Annotation similar to bind.annotation.XmlRootElement, used to indicate name to use for root-level wrapping, if wrapping is enabled.

pgiecek
  • 7,970
  • 4
  • 39
  • 47