我有一个SOAP Web服务,我试图在应用程序内部调用。我使用cxf-codegen-plugin(3.1.10)从WSDL生成源代码。
使用生成的客户端,如果我在应用程序中调用webservice,它的效果很好。但是,我还在应用程序中使用另一个JAXB实例来引发问题。
例如,以下效果很好:
OutboundServicePortType service = new OutboundService().getOutboundServicePort();
service.sendMessage(message);
但是,在之前初始化新的JAXB实例会导致getOutboundServicePort()
调用失败:
JAXBContext.newInstance(SendMessageRequest.class);
OutboundServicePortType service = new OutboundService().getOutboundServicePort();
service.sendMessage(message);
使用以下stacktrace:
Caused by: java.lang.ClassCastException: outbound.model.standard.StandardOutboundMessage$JaxbAccessorF_messageUUId cannot be cast to com.sun.xml.internal.bind.v2.runtime.reflect.Accessor
at com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.instanciate(OptimizedAccessorFactory.java:190)
at com.sun.xml.internal.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:179)
at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:271)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementLeafProperty.<init>(SingleElementLeafProperty.java:77)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:166)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:488)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:153)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:488)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:305)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:152)
at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:96)
at com.sun.xml.internal.ws.developer.JAXBContextFactory$1.createJAXBContext(JAXBContextFactory.java:98)
at com.sun.xml.internal.ws.db.glassfish.JAXBRIContextFactory.newContext(JAXBRIContextFactory.java:79)
... 25 more
到目前为止我尝试过的事情:
JAXB classes from Webservice marshalling error
Problems creating JAXBContext to marshal object as XML
System.setProperty( "com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize", "true");
。但是,在我的环境中,不幸的是,设置此属性不是我的选择。此外,它似乎有点像黑客并没有解决真正的问题(除非我误解了它)。我要用我留下的小绳子挂上自己。我在这里缺少什么?
答案 0 :(得分:10)
我讨厌回答我自己的问题,但我想确保我最终得到的解决方案清楚地记录下来。
根本问题是camel-jaxb引入的jaxb-impl jar与JDK 8提供的版本冲突。
This answer更清楚地描述了正在发生的事情:
当我尝试将JAXB升级到更新版本时遇到了同样的错误 版本比JDK附带的版本。 Java遇到两个或更多 运行时JAXB的实例,无法决定使用哪个版本 使用
就我而言,我只是排除了camel-jaxb附带的jaxb-impl,并且应用程序开始正常工作。
答案 1 :(得分:0)
我在测试范围中遇到了同样的问题。对我来说,添加jaxws-rt运行时jar帮助了。请注意,我仅在测试范围内添加了它。您不打算在产品运行时执行此操作,因为JEE容器应该已经具有此实现或其他实现。
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.3</version>
<scope>test</scope>
</dependency>
答案 2 :(得分:0)
即使通过Eclipse在本地传递尝试通过Maven运行JUnit测试时,我也遇到此错误。我的解决方案是放在@BeforeClass中:
System.setProperty(“ javax.xml.bind.JAXBContext”,“ com.sun.xml.internal.bind.v2.ContextFactory”);
答案 3 :(得分:-1)
我没有足够的代表对此发表评论-但想强调一下,早期的简单修复非常棒。排除“ camel-jaxb随附的jaxb-impl”。 我的错误是
ngOnInit() {
let maxFileSize = 5 * 1024 * 1024; // modify this to your desired max file size
this.uploader = new FileUploader({
url:this.url, removeAfterUpload: false,
autoUpload: false ,
method:'post',
maxFileSize:maxFileSize
});
this.uploader.onWhenAddingFileFailed = (item, filter) => {
let message = '';
switch (filter.name) {
case 'fileSize':
message = 'Warning ! \nThe uploaded file \"' + item.name + '\" is ' + this.formatBytes(item.size) + ', this exceeds the maximum allowed size of ' + this.formatBytes(maxFileSize);
break;
default:
message = 'Error trying to upload file '+item.name;
break;
}
alert(message);
};
}
formatBytes(bytes, decimals?) {
if (bytes == 0) return '0 Bytes';
const k = 1024,
dm = decimals || 2,
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}