import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class MathUtils {
@WebMethod
public int add(int a, int b) {
return a + b;
}
@WebMethod
public int subtract(int a, int b) {
return a - b;
}
}
shell
$ java javax.xml.rpc.encoding.TypeMappingImpl
<web-app>
<servlet>
<servlet-name>MathUtilsService</servlet-name>
<servlet-class>com.example.MathUtils</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MathUtilsService</servlet-name>
<url-pattern>/services/MathUtils</url-pattern>
</servlet-mapping>
</web-app>
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
public class MathUtilsClient {
public static void main(String[] args) throws Exception {
String endpointUrl = "http://localhost:8080/services/MathUtils";
URL wsdlUrl = new URL(endpointUrl + "?wsdl");
QName serviceName = new QName("http://example.com/", "MathUtilsService");
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(wsdlUrl, serviceName);
MathUtils mathUtils = (MathUtils) service.getPort(MathUtils.class);
int result = mathUtils.add(10, 5);
System.out.println("Result: " + result);
}
}