gpt4 book ai didi

jaxb - CXF JAXB 数据绑定(bind)问题 - java.io.Serializable 是一个接口(interface),而 JAXB 无法处理接口(interface)

转载 作者:行者123 更新时间:2023-12-04 18:20:27 27 4
gpt4 key购买 nike

我有以下代码

@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;
}
}

和一个简单的网络服务界面,

@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

I got following exception

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参数传递?

最佳答案

那是行不通的。 Serializable不能由 JAXB 处理,这是 CXF 在处理 Java 和 XML 之间的实际转换时使用的绑定(bind)引擎。

要修复,您必须决定是否需要 id XML 中的属性。如果你不这样做,只需用 @XmlTransient 标记 getter会让事情顺利进行。但是,如果您确实想要一个 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;
}
}

关于jaxb - CXF JAXB 数据绑定(bind)问题 - java.io.Serializable 是一个接口(interface),而 JAXB 无法处理接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10798310/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com