WSDL远程调用。

上次刚刚提到WSDL的介绍及如何创建WSDL文件及本地文件生成并解析。WSDL详解
接下来,以下内容为远程调用webServic的方法。

发布服务

以下发布服务的代码与上次写的一样,但是TestWsdl类有了一些变化。

package com.test.wsdl;
import javax.xml.ws.Endpoint;

public class Test {
    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8099/test",new TestWsdl());
    }
}

TestWsdl如下,注意要加@WebService,@WebMethod,@WebParam注解

package com.test.wsdl;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace = "http://wsdl.test.com/")
public class TestWsdl {

    @WebMethod(action = "http://wsdl.test.com/testString")
    public String testString(@WebParam(name = "str",targetNamespace = "http://wsdl.test.com/") String  str){
        return "这个是TestString方法的返回内容,这个是你输入的值"+str;
    }
}

调用代码

pom依赖

我在处理过程中,出现了依赖冲突,依赖版本与代码不对应的情况,所以直接将全部用到的依赖全部放到下边。

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.3.3</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.2.4</version>
			<exclusions>
				<exclusion>
					<artifactId>cxf-rt-transports-http</artifactId>
					<groupId>org.apache.cxf</groupId>
				</exclusion>
				<exclusion>
					<artifactId>cxf-core</artifactId>
					<groupId>org.apache.cxf</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
		<dependency>
			<groupId>org.apache.axis</groupId>
			<artifactId>axis</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.axis</groupId>
			<artifactId>axis-jaxrpc</artifactId>
			<version>1.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
		<dependency>
			<groupId>commons-discovery</groupId>
			<artifactId>commons-discovery</artifactId>
			<version>0.2</version>
		</dependency>

		<!--    webservice    -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
			<version>3.3.3</version>
			<exclusions>
				<exclusion>
					<artifactId>cxf-rt-frontend-jaxws</artifactId>
					<groupId>org.apache.cxf</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.12</version>
			<exclusions>
				<exclusion>
					<artifactId>cxf-core</artifactId>
					<groupId>org.apache.cxf</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.24</version>
			<scope>provided</scope>
		</dependency>
	
	</dependencies>

调用的时候,也出现引错包导致一直执行不成功的情况,所以将全部的代码贴在下边。

1.首先先定义枚举类,将服务地址,命名空间,方法都可以定义在枚举类中,方便更改名称与统一管理。

package com.test.wsdl;

import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum WSDLEnum {

    SERVICE_URL("http://localhost:8099/test?wsdl","服务URL"),
    NAMESPACE_URI("http://wsdl.test.com/","命名URI"),
    TEST_STRING_METHOD("testString","teseString方法");
    private String name;
    private String describe;
}

2.客户端

package com.test.wsdl;

import org.apache.axis.Constants;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
 
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

public class WebServiceTest {
 
    public static void main(String[] args) {
        //服务地址
        String url = WSDLEnum.SERVICE_URL.getName();
        //命名空间
        String namespaceUri = WSDLEnum.NAMESPACE_URI.getName();
        //方法名
        String method = WSDLEnum.TEST_STRING_METHOD.getName();
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            //设置要调用的方法
            call.setOperationName(new QName(namespaceUri, method));
            //设置要返回的数据类型
            call.setReturnType(new QName(namespaceUri, method), String.class);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(namespaceUri + method);
            //设置入参
            call.addParameter(new QName(namespaceUri, "str"), Constants.XSD_STRING, ParameterMode.IN);
            //调用方法并传递参数
            String resultStr = (String) call.invoke(new Object[]{ new String("123")});
            System.out.println("服务调用结果:" + resultStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

WSDL远程调用。_第1张图片

出现以下展示则调用成功。

你可能感兴趣的:(Java基础,java,jetty)