webservice的日常:webservice的测试

webservice 的日常:webservice的测试

1.springboot集成WS.
2.springboot集成CXF.

简介

这里针对springboot集成的ws和cxf方式进行调用测试

postman

正常模式:有request与response

设置请求:POST
设置header:
Content-type:application/soap+xml;
设置报文:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
	<soap:Header>
	</soap:Header>
	<soap:Body>
		<ns2:HelloRequest xmlns:ns2="http://service.hello.com">
        <ns2:HelloReq>
           <username>123</username>
        </ns2:HelloReq>
    </ns2:HelloRequest>
	</soap:Body>
</soap:Envelope>

然后发起请求即可。

其他:无request-1

设置请求:POST
设置header:
Content-type:application/soap+xml; action=“urn:helloNoRequest”; charset=UTF-8

说明:这里设置了urn为请求的action方法,通过实现filter过滤器实现拦截处理
请求报文同上,然后发起请求即可

其他:无request-2

设置请求:POST
设置header:上述两种都可以
报文:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
	<soap:Header>
	</soap:Header>
	<soap:Body>
		<ns2:HelloNoRequest xmlns:ns2="http://service.hello.com"/>
	</soap:Body>
</soap:Envelope>

说明:这里设置了body的namespace,相当于指定了方法。
然后发起请求即可

编码模式:

http:

直接拼接报文,设置header请求:

public static void main(String[] args) {
    String uri = "http://localhost:8081/hello/HelloApi.wsdl";
    try {
        URL url = new URL(uri);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        // httpURLConnection.setRequestProperty("content-type", "text/xml;charset=utf-8"); soap1.1
        httpURLConnection.setRequestProperty("content-type", "application/soap+xml;charset=UTF-8");// soap1.2
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        String soapXML = getXML();
        OutputStream outputStream = httpURLConnection.getOutputStream();
        outputStream.write(soapXML.getBytes());
        int responseCode = httpURLConnection.getResponseCode();
        if (responseCode == 200) {
            BufferedReader bufferedReader =
                new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            StringBuilder stringBuilder = new StringBuilder();
            String temp;
            while ((temp = bufferedReader.readLine()) != null) {
                stringBuilder.append(temp);
            }
            log.info("result:{}", stringBuilder.toString());
            bufferedReader.close();
        } else {
            log.info("resultCode:{}", responseCode);
        }
        outputStream.close();
        httpURLConnection.disconnect();
    } catch (Exception e) {
        log.info("error:{}", e.getMessage());
    }
}

public static String getXML() {
    String soapXML =
        "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">"
            + "  <soap:Header>"
            + "  </soap:Header>"
            + "  <soap:Body >"
             + "    <ns2:HelloRequest xmlns:ns2=\"http://service.hello.com\">"
            + "      <ns2:HelloReq>"
            + "        <username>test</username>"
            + "      </ns2:HelloReq>"
            + "    </ns2:HelloRequest>"
            + "  </soap:Body>"
            + "</soap:Envelope>";
    return soapXML;
}
webservice:

直接使用ws自带的template请求:

String url = "http://localhost:8081/hello/HelloApi.wsdl";
ObjectFactory objFac = new ObjectFactory();
try {
    WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
    webServiceTemplate.setDefaultUri(url);
    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    jaxb2Marshaller.setClassesToBeBound(HelloRequest.class, HelloResponse.class);
    webServiceTemplate.setMarshaller(jaxb2Marshaller);
    webServiceTemplate.setUnmarshaller(jaxb2Marshaller);
    HelloRequest helloRequest = new HelloRequest();
    // helloRequest.setHelloReq();
    HelloResponse response = (HelloResponse) webServiceTemplate.marshalSendAndReceive(helloRequest);
} catch (Exception e) {
    log.info("error:{}", e.getMessage());
}

其中ObjectFactory为xjc生成的配置对象,为了设置JAXBElement类型数据

补充:camel客户端

camel通过factory创建template,设置context上下文内容(包含header、方法名、请求类型、请求内容等)
然后发起请求。
camel调用webservice.

githup:
webservice源码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值