java调用webservice接口

pom.xml

<dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>

        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>

application.properties

erp.webservice.targetEndpointAddress=http://localhost:8084/demo/api?wsdl
erp.webservice.nameSpaceURI=http://server.webservice.main.mes.kaicom.com
erp.webservice.localPart=test

java代码

import com.kaicom.mes.main.util.DateUtil;
import com.kaicom.mes.util.GsonUtil;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import java.net.URL;
import java.util.Date;


/**
 * @Author: BillYu
 * @Description:
 * @Date: Created in 10:43 2019-11-01.
 */

@org.springframework.stereotype.Service
public class SendData {

    @Autowired
    private Environment env;

    /**
     * 调用webservice接口
     * @param message
     * @return
     */
    public String requestWebserviceApi(String message) {
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            // 命名空间
            String endpoint = env.getProperty("erp.webservice.targetEndpointAddress");
            call.setTargetEndpointAddress(new URL(endpoint));
            call.setOperationName(new QName(env.getProperty("erp.webservice.nameSpaceURI"),env.getProperty("erp.webservice.localPart")));  //设置要调用的接口命名空间和方法
            call.addParameter("json", XMLType.SOAP_STRING, ParameterMode.IN);
            String param1 = "[{\"name\":\"test\"}]";  // 参数
            call.setReturnType(XMLType.SOAP_STRING);  // 设置返回值
            //超时时间5秒
            call.setTimeout(5000);
            String result = "";
            result = (String) call.invoke(new Object[]{param1}); // 调用获取返回值
            return result;
        } catch (
                Exception e) {
            e.printStackTrace();
        }
        return "netError";
    }

}
发表新评论