Server
Server using Spring4 MVC technology, maven project
Add dependent
Writing service interface
/** * @author WGR * @create 2020/3/9 -- 20:20 */ public interface HelloService { public String sayHello(String info); }
/** * @author WGR * @create 2020/3/9 -- 20:22 */ public class HelloServiceImpl implements HelloService { @Override public String sayHello(String info) { return "sayHello:"+info; } }
Configure the server
The service side of the spring configuration file applicationContext.xml:
And then configure the services.xml, this is the key.
Established under the / WEB-INF / directory directory / services / axis / META-INF, and then create a file services.xml in this directory, as follows:
Then configure web.xml:
Start the test
After starting Spring MVC application, enter in the browser: HTTP: // localhost: 8080 / Services / wsdl the HelloWorld?
Correct return the WSDL file, and there will be a service I defined:
Server completed successfully!
Client
import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; /** * @author WGR * @create 2020/3/10 -- 22:27 */ public class HelloClient { private static String url = "http://localhost:8080/services/HelloWorld"; public static void main(String[] args) { testSayHi(); } private static void testSayHi() { try { RPCServiceClient client = new RPCServiceClient(); Options options = client.getOptions(); EndpointReference endpoint = new EndpointReference(url); options.setTo(endpoint); QName qname = new QName("http://impl.service", "sayHello"); Object[] methodArgs = new Object[] { "Tom" }; Class<?>[] returnType = new Class[] { String.class }; String result = (String) client.invokeBlocking(qname, methodArgs, returnType)[0]; System.out.println(result); } catch (AxisFault e) { e.printStackTrace(); } } }