Java请求webservice踩过的坑

最近项目对接过程中,因为对方系统比较旧,我们和对方进行交互使用webservice方式进行,对方给出相关文档,

接口地址:http://ip:port/abc/def/xxxService?wsdl

接口名称:methodA

1-springboot配合CXF使用

由于接口的ip是内网地址,我们业务服务器出去,我们必须中转到一台可以打通内网ip的机器A上才能请求。首先我们使用内网穿透工具(ngfork),在A上部署一个中转应用。

大概流程如下:

 

Java核心代码如下:

pom引入依赖

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/>
    </parent>


<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-ri -->
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-ri</artifactId>
            <version>2.3.3</version>
            <type>pom</type>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.2.0.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web-services -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.4.3</version>
        </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.4.3</version>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.30</version>
        </dependency>


    </dependencies>

核心java代码如下:

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("接口地址");
Object[] objects = client.invoke("接口名称", reqStr);

objects就是对方返回的结果。

这种情况直接使用ip,完全没有问题,如下使用域名会出现如下错误:

org.apache.cxf.interceptor.Fault: Response was of unexpected text/html ContentType.  Incoming portion of HTML stream: <html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>

 

2-使用AXIS请求

上面使用frps进行时,使用域名会出现以上错误,我们更换一种请求方式,就可以获取到结果。方式二直接需要一台公网服务器部署frps,需要域名;然后在中转机上不是frpc客户端,配置相关内容,不需要部署一个服务,直接域名穿透过去。

pom依赖:

 <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>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

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

        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.5.2</version>
        </dependency>

核心java代码:

//服务地址
        String url = "https://xxx.com/abc/def/xxxService?wsdl";
        //命名空间
        String namespaceURI = XMLConstants.NULL_NS_URI;
        //方法名
        String method = "yourMethod";
        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, "params"), Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);

            //调用方法并传递参数
            String resultStr = (String) call.invoke(new Object[]{"{\"name\":\"zhangsan\",\"pwd\":\"abc123456\"}"});
            System.out.println("服务调用结果:" + resultStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

使用方式二就没有出现方式一的301错误。问题解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java发出请求WebService,你可以按照以下步骤进行操作: 1. 导入必要的库文件:在Java项目中,你需要导入相应的库文件,以便能够使用WebService相关的类和方法。具体的库文件取决于你使用的WebService框架,例如JAX-WS或Apache Axis等。 2. 创建客户端代理:使用WebService提供的WSDL(Web服务描述语言)文件,通过客户端代理工具生成客户端代码。这个代理类将充当与WebService进行交互的中间层。 3. 初始化WebService客户端:在Java代码中,你需要实例化WebService客户端类,并进行相应的配置。这包括设置WebService的地址、命空间和其他参数。 4. 调用WebService方法:通过客户端代理类的实例,你可以直接调用WebService提供的方法。根据WebService的定义,可能需要提供相应的参数,并接收返回值。 5. 处理返回结果:根据具体需要,你可以对返回结果进行处理和解析。这可能涉及到XML解析、数据转换等操作。 下面是一个简单示例,用于展示如何使用Java发送SOAP请求到一个Web服务: ```java import javax.xml.namespace.QName; import javax.xml.soap.*; import java.io.ByteArrayOutputStream; public class WebServiceClient { public static void main(String[] args) { try { // 创建SOAP消息 SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); // 创建SOAP请求 SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration("web", "http://example.com/webservice"); SOAPBody soapBody = envelope.getBody(); QName operationName = new QName("http://example.com/webservice", "getHelloWorld"); SOAPBodyElement soapBodyElement = soapBody.addBodyElement(operationName); // 设置请求参数(如果有) QName parameterName = new QName("http://example.com/webservice", "name"); SOAPElement parameterElement = soapBodyElement.addChildElement(parameterName); parameterElement.setValue("John"); // 发送请求 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); String url = "http://example.com/webservice"; SOAPMessage response = soapConnection.call(soapMessage, url); // 处理响应 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); response.writeTo(outputStream); String responseString = outputStream.toString(); // 打印响应结果 System.out.println("Response:\n" + responseString); // 关闭连接 soapConnection.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这只是一个基本示例,实际上,你需要根据具体的WebService和框架进行相应的配置和参数设置。希望这能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值