WS-I 概要文件中不允许方法重载。通常的技巧似乎是添加@WebMethod(operationName="...")
注释。如果我这样做,我会得到一个 ClassCastException。
最小的例子如下。
界面:
package org.example.test;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService(name = "service", targetNamespace = "http://www.example.org/test")
public interface IThing {
@WebMethod(operationName = "f1")
public String f(String s);
@WebMethod(operationName = "f2")
public String f(Integer i);
}
执行:
package org.example.test;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService(endpointInterface = "org.example.test.IThing", targetNamespace = "http://www.example.org/test", portName = "ThingPort", serviceName = "service")
public class Thing implements IThing {
@Override
@WebMethod(operationName = "f1")
public String f(String s) {
return "f1";
}
@Override
@WebMethod(operationName = "f2")
public String f(Integer i) {
return "f2";
}
}
JUnit 测试用例:
package org.example.test;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import org.junit.Test;
public class TestThing {
@Test
public void testThing() throws Exception {
URL urlURL = new URL("http://localhost:8080/wstest/SOAP");
QName qname = new QName("http://www.example.org/test", "service");
Service service = Service.create(urlURL, qname);
IThing thing = service.getPort(IThing.class);
thing.f("s");
thing.f(1);
}
}
使用的 JAX-WS 实现:
compile group:'com.sun.xml.ws', name:'jaxws-rt', version:'2.2.7', transitive:true
轨迹轨迹:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at org.example.test.jaxws.F$JaxbAccessorF_arg0.set(FieldAccessor_Ref.java:60)
at com.sun.xml.bind.v2.runtime.reflect.Accessor.setUnadapted(Accessor.java:166)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$6.set(JAXBContextImpl.java:980)
at com.sun.xml.ws.db.glassfish.RawAccessorWrapper.set(RawAccessorWrapper.java:73)
at com.sun.xml.ws.client.sei.BodyBuilder$DocLit.build(BodyBuilder.java:264)
at com.sun.xml.ws.client.sei.BodyBuilder$JAXB.createMessage(BodyBuilder.java:103)
at com.sun.xml.ws.client.sei.StubHandler.createRequestPacket(StubHandler.java:231)
at com.sun.xml.ws.db.DatabindingImpl.serializeRequest(DatabindingImpl.java:195)
at com.sun.xml.ws.db.DatabindingImpl.serializeRequest(DatabindingImpl.java:257)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:117)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:102)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:154)
at com.sun.proxy.$Proxy31.f(Unknown Source)
at org.example.test.TestThing.testThing(TestThing.java:19)
运行环境是Tomcat7。
我假设我也没有被迫重命名 Java 方法。我是吗?
包含 Eclipse 项目的 zip 可在http://ge.tt/1JrLxFh/v/0获得