JAX-RS / Jersey:java.lang.ClassCastException - 无法强制转换为javax.xml.bind.JAXBElement

时间:2014-01-03 11:25:36

标签: java web-services jersey jax-rs

我将应用程序分隔到前端和后端模块,这些模块通过restfull webservice进行通信。不幸的是,这段代码出了问题,我从Backend部分得到了:

java.lang.ClassCastException: com.rrd.ecomdd.data.SharedFile cannot be cast to javax.xml.bind.JAXBElement 

前端片段:

@Override
public void share(Set<SharedFile> fileSet) {
    apiTarget.path(ApiConstant.FILESERVICE)
            .path(ApiConstant.FILESERVICE_SHARE)
            .request(MediaType.APPLICATION_JSON_TYPE.withCharset("UTF-8"))
            .post(Entity.entity(fileSet.toArray(new SharedFile[0]), MediaType.APPLICATION_JSON_TYPE.withCharset("UTF-8")), new GenericType<Set<SharedFile>>() {
            });
}

后端代码段

@POST
@Path(ApiConstant.FILESERVICE_SHARE)
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public List<SharedFile> share(SharedFile[] sharedList) {
    for (SharedFile s : sharedList) {
        fileService.share(s);
    }
    return Arrays.asList(sharedList);
}

SharedFile类:

public class SharedFile {

  private Long id;
  private User user;
  private ManagedFile file;
  private UUID uuid = UUID.randomUUID();

  public SharedFile(User user, ManagedFile file) {
    this.user = user;
    this.file = file;
  }

  public SharedFile() {
  }
  //getters, setters, equals and hashcode below

}

任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

尝试注释这里提到的类及其属性:

@XmlRootElement
public class SharedFile {

  @XmlElement
  private Long id;

  @XmlElement
  private User user;

  @XmlElement
  private ManagedFile file;

请点击此处了解更多信息:http://docs.oracle.com/javaee/6/tutorial/doc/gkknj.html