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
Strack trace:
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答案 0 :(得分:1)
您还需要确保区分参数。否则,模式会将相同的类型与两种方法的参数相关联,从而有效地破坏了您的重载意图。
@Override
@WebMethod(operationName = "f2")
@ResponseWrapper(className = "f2Response")
@RequestWrapper(className = "f2")
public String f(Integer i) {
return "f2";
}