【问题标题】:RESTeasy client : send xml data POST = unexpected element (uri="", local="paint")RESTeasy 客户端:发送 xml 数据 POST = 意外元素 (uri="", local="paint")
【发布时间】:2012-12-24 15:30:51
【问题描述】:

我想通过 POST 请求向服务器发送一个序列化的对象。

        Paint paint = new Paint();
        paint.setYear(2010);
        paint.setTitle("title");


        JAXBContext jc = JAXBContext.newInstance(Paint.class, Art.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        java.io.StringWriter sw = new StringWriter();
        marshaller.marshal(paint, sw);

        ClientRequest request = new ClientRequest("http://localhost:8080/rest/createArt");

        request.body("application/xml", sw.toString());

        ClientResponse<Response> response = request.post(Response.class);

对于这个简单的服务:

@POST
@Path("/createArt")
@Consumes("application/xml")
public Response createArt(Art a){
    artDAO.createArt(a);
    return Response.ok(a).build();
}

但我收到以下错误:

DefaultClientConnection:249 - Receiving response: HTTP/1.1 400 javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"paint"). Expected elements are <{}art>, <{}photo>

为什么要开设这些课程?

Art.java : 抽象类

@Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@XmlRootElement(name = "art")
public abstract class Art {

@Id @GeneratedValue(strategy = GenerationType.AUTO) 
private int id;

@OneToMany (mappedBy="art",cascade={CascadeType.PERSIST, CascadeType.REMOVE})
private List<Photo> photo;
...
}

Paint.java : 继承自 Art

@Entity
@XmlRootElement(name = "paint")
public class Paint extends Art{
@Enumerated(EnumType.STRING)
private SupportArt support;
@Enumerated(EnumType.STRING) 
private Realisation realisation;
...
}

Photo.java

@Entity
@XmlRootElement(name = "photo")
public class Photo {

@Id @GeneratedValue(strategy = GenerationType.AUTO) 
private int id;

@ManyToOne 
@JoinColumn (name="art_id")
private Art art;

private String path;
...
}

我不明白我的错误,我认为这是 JAXB 注释的问题。 继承也给我带来了一些麻烦。 照片和艺术之间存在双向关系,当我删除它时,照片不再在预期的元素中(它持续 )。

我在网上搜索并尝试了很多东西,但都没有解决我的问题。

【问题讨论】:

    标签: xml inheritance post jaxb resteasy


    【解决方案1】:

    我发现了我的错误,我只需要使用 XmlSeeAlso :

    Art.java : 抽象类

    @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @XmlSeeAlso(Paint.class)
    public abstract class Art {
    

    也不得不修改这一行:

    ClientResponse<String> response = request.post(String.class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 2012-08-30
      相关资源
      最近更新 更多