REST服务返回JSON实体而没有根级别换行

时间:2014-07-16 09:27:43

标签: json web-services rest jackson

我正在开发Java EE项目,我使用Jackson部署了我的Rest Webservices。服务工作正常,但我遇到的一个问题是我没有得到根名称:

这是一个例子:


[{"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 "}]

知道我已定义:@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与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>

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

默认情况下,仅核心Jackson注释用于配置数据绑定方面。要启用JAXB注释支持,您需要:

  1. 包括jackson-xc jar,其中包含org.codehaus.jackson.xc.JaxbAnnotationIntrospector(Jackson 1.x) 或者,com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector(杰克逊2.x)

  2. 注册此注释内省运动员

  3. 您可以找到更多详情here

    杰克逊替代@XmlRootElement注释的是@JsonRootName

      

    类似于bind.annotation.XmlRootElement的注释,用于指示用于根级别包装的名称,如果启用了包装。