CXF JAXB数据绑定问题 - java.io.Serializable是一个接口,JAXB无法处理接口

时间:2012-05-29 11:33:45

标签: jaxb cxf

我有以下代码

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SamplePojo {
    private Serializable id;
    private String name;

    public Serializable getId() {
        return id;
    }
    public void setId(Serializable id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
    this.name = name;
    }
}

和一个简单的Web服务接口,

@WebService
public interface SampleManagementService {
    @WebMethod
    @WebResult(name = "samplePojo")
    public SamplePojo update(@WebParam(name = "samplePojo") SamplePojo pojo) throws SampleException;
}

我正在使用CXF-2.3.6和org.apache.cxf.tools.java2ws.JavaToWS从java代码生成WSDL。当我运行命令时

java2ws -verbose -o sample.wsdl -wsdl -frontend jaxws com.test.cxf.webservice.sample.SampleManagementService

我遇到了以下异常

cxfJavaToWSDL:
     [java] java2ws -verbose -o sample.wsdl -wsdl -frontend jaxws com.test.cxf.webservice.sample.SampleManagementService
     [java] java2ws - Apache CXF 2.3.6
     [java]
     [java] May 29, 2012 4:51:48 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
     [java] INFO: Creating Service {http://sample.webservice.cxf.test.com/}SampleManagementServiceService from class com.test.cxf.webservice.sample.SampleManagementService
     [java] Error: java.lang.RuntimeException: org.apache.cxf.service.factory.ServiceConstructionException
     [java]
     [java] java.lang.RuntimeException: org.apache.cxf.service.factory.ServiceConstructionException
     [java]     at org.apache.cxf.frontend.AbstractServiceFactory.createService(AbstractServiceFactory.java:80)
     [java]     at org.apache.cxf.tools.java2wsdl.processor.JavaToWSDLProcessor.process(JavaToWSDLProcessor.java:101)
     [java]     at org.apache.cxf.tools.java2ws.JavaToWSContainer.processWSDL(JavaToWSContainer.java:110)
     [java]     at org.apache.cxf.tools.java2ws.JavaToWSContainer.execute(JavaToWSContainer.java:75)
     [java]     at org.apache.cxf.tools.common.toolspec.ToolRunner.runTool(ToolRunner.java:103)
     [java]     at org.apache.cxf.tools.common.toolspec.ToolRunner.runTool(ToolRunner.java:58)
     [java]     at org.apache.cxf.tools.common.toolspec.ToolRunner.runTool(ToolRunner.java:40)
     [java]     at org.apache.cxf.tools.java2ws.JavaToWS.run(JavaToWS.java:77)
     [java]     at org.apache.cxf.tools.java2ws.JavaToWS.main(JavaToWS.java:45)
     [java] Caused by: org.apache.cxf.service.factory.ServiceConstructionException
     [java]     at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:341)
     [java]     at org.apache.cxf.service.factory.AbstractServiceFactoryBean.initializeDataBindings(AbstractServiceFactoryBean.java:86)
     [java]     at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.buildServiceFromClass(ReflectionServiceFactoryBean.java:442)
     [java]     at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.buildServiceFromClass(JaxWsServiceFactoryBean.java:682)
     [java]     at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.initializeServiceModel(ReflectionServiceFactoryBean.java:505)
     [java]     at org.apache.cxf.service.factory.ReflectionServiceFactoryBean.create(ReflectionServiceFactoryBean.java:242)
     [java]     at org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean.create(JaxWsServiceFactoryBean.java:202)
     [java]     at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:101)
     [java]     at org.apache.cxf.frontend.AbstractServiceFactory.createService(AbstractServiceFactory.java:78)
     [java]     ... 8 more
     [java] Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions
     [java] Adapter com.test.cxf.webservice.sample.SerializableAdapter is not applicable to the field type java.io.Serializable.
     [java]     this problem is related to the following location:
     [java]             at @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(type=class javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter$DEFAULT, value=class com.test.cxf.webservice.sample.SerializableAdapter)
     [java]             at private java.io.Serializable com.test.cxf.webservice.sample.SamplePojo.id
     [java]             at com.test.cxf.webservice.sample.SamplePojo
     [java]             at private com.test.cxf.webservice.sample.SamplePojo com.test.cxf.webservice.sample.jaxws_asm.Update.samplePojo
     [java]             at com.test.cxf.webservice.sample.jaxws_asm.Update
     [java] java.io.Serializable is an interface, and JAXB can't handle interfaces.
     [java]     this problem is related to the following location:
     [java]             at java.io.Serializable
     [java]             at private java.io.Serializable com.test.cxf.webservice.sample.SamplePojo.id
     [java]             at com.test.cxf.webservice.sample.SamplePojo
     [java]             at private com.test.cxf.webservice.sample.SamplePojo com.test.cxf.webservice.sample.jaxws_asm.Update.samplePojo
     [java]             at com.test.cxf.webservice.sample.jaxws_asm.Update
     [java] java.io.Serializable does not have a no-arg default constructor.
     [java]     this problem is related to the following location:
     [java]             at java.io.Serializable
     [java]             at private java.io.Serializable com.test.cxf.webservice.sample.SamplePojo.id
     [java]             at com.test.cxf.webservice.sample.SamplePojo
     [java]             at private com.test.cxf.webservice.sample.SamplePojo com.test.cxf.webservice.sample.jaxws_asm.Update.samplePojo
     [java]             at com.test.cxf.webservice.sample.jaxws_asm.Update
     [java]
     [java]     at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:102)
     [java]     at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:472)
     [java]     at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:302)
     [java]     at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1140)

我试图创建SerializableAdapter但没有运气。任何人都可以告诉我处理Serializable参数传递的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

那不会奏效。 Serializable无法由JAXB处理,JAXB是处理Java和XML之间实际转换时CXF使用的绑定引擎。

要修复,您必须决定是否要在XML中使用id属性。如果你不这样做,只需用@XmlTransient标记吸气剂就可以了。但是,如果你想要一个id,那么你必须决定它将如何映射到一个字符串(如果该字段是一个具体的类而不仅仅是任何一个字段,这将更简单任意可序列化的东西)。如果你有/ String的转换效果很好,你只需将你的属性更改为字符串属性或制作虚假属性:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE) // Note the change here too!
public class SamplePojo {
    private Serializable id;
    private String name;

    @XmlTransient
    public Serializable getId() {
        return id;
    }
    public void setId(Serializable id) {
        this.id = id;
    }

    @XmlElement(name = "id")
    public String getStringId() {
        return id.toString(); // Or whatever...
    }
    public void setStringId(String id) {
        this.id = IdFactory.getIdFromString(id); // Or whatever...
    }

    @XmlElement
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

答案 1 :(得分:1)

您可以在此用例中使用XmlAdapter。您需要指定一个可映射的值类型(在下面的示例中为String),然后提供必要的转换逻辑:

<强> SerializableAdapter

package forum10798310;

import java.io.Serializable; import javax.xml.bind.annotation.adapters.XmlAdapter;

public class SerializableAdapter extends XmlAdapter<String, Serializable>{

    @Override
    public Serializable unmarshal(String v) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String marshal(Serializable v) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}

<强> SamplePojo

@XmlJavaTypeAdapter注释用于链接XmlAdapter

package forum10798310;

import java.io.Serializable;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SamplePojo {
    @XmlJavaTypeAdapter(SerializableAdapter.class)
    private Serializable id;
    private String name;

    public Serializable getId() {
        return id;
    }
    public void setId(Serializable id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
    this.name = name;
    }
}