Java 调用SAP PO 的Rest接口
Posted Star_SAP
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 调用SAP PO 的Rest接口相关的知识,希望对你有一定的参考价值。
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、SAP PI PO 是什么?
- 二、完整JAVA示例代码,运行通过
前言
接口开发完成,先自己测试通过。
常用的测试工具有SoapUI,Postman,XmlSpy 等。
但有时接口调用方不会调用,就直接咬定接口是有问题的,接口是不通的。甚至会说接口只支持测试工具,不支持他们的开发语言。互相扯皮很伤感情,这时需要一段能运行出正常结果的示例代码
提示:以下是本篇文章正文内容,下面案例可供参考
一、SAP PI PO 是什么?
What is SAP Process Orchestration?
SAP Process Orchestration software supports custom process applications and integration scenarios. As the process orchestration layer of SAP's Business Technology Platform, it can help you improve process efficiencies and respond to changing demands.
————————————————
SAP Process Orchestration
Model, implement, integrate, and monitor custom process applications and integration scenarios – quickly and flexibly. By creating more streamlined, adaptable processes, you can innovate faster and respond better to changing demands.
On-premise deployment
Faster modeling and deployment of business process applications
Lower costs related to integrating heterogeneous systems
Improved data quality and reduced data entry effort
————————————————
二、完整JAVA示例代码,运行通过
代码如下(示例):
package com.rest;
/*
* Java 调用SAP PI PO 的Rest JSON接口
* https://mkyong.com/java/how-to-get-http-response-header-in-java/
* https://www.jianshu.com/p/375be5929bed
*/
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Base64;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.*;
public class RunREST
static final String kuser = "USER"; //用户名 请注意用户 密码一定要正确,否则会报 401 unauthorized
static final String kpass = "PWD"; //密码
static class MyAuthenticator extends Authenticator
public PasswordAuthentication getPasswordAuthentication()
return (new PasswordAuthentication(kuser, kpass.toCharArray()));
public static String httpClientWithBasicAuth(String username, String password, String uri, JSONObject param)
try
//创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("Content-Type", "application/json; charset=UTF-8"); //UTF-8 防中文乱码
//添加Http头信息
httpPost.addHeader("Authorization",
"Basic " + Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));
httpPost.setEntity(new StringEntity(param.toString(), Charset.forName("UTF-8"))); //UTF-8 防中文乱码
String result = "";
HttpResponse httpResponse = null;
HttpEntity entity = null;
try
httpResponse = closeableHttpClient.execute(httpPost);
entity = httpResponse.getEntity();
if (entity != null)
result = EntityUtils.toString(entity);
catch (ClientProtocolException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
String httpcode = String.valueOf(httpResponse.getStatusLine().getStatusCode());
System.out.println("Response.getStatusLine():" + httpResponse.getStatusLine());
System.out.println("Http status:" + httpcode);
Header[] headers = httpResponse.getAllHeaders();
for (Header header : headers)
System.out.println("Key:" + header.getName() + " ,Value:" + header.getValue());
System.out.println(Arrays.toString(httpResponse.getAllHeaders()));
//关闭连接
closeableHttpClient.close();
return result;
catch (Exception e)
return "Error.";
public static void main(String[] args) throws Exception
String url = "http://<host>:<port>/RESTAdapter/Endpoint"; //实际的接口地址, Endpoint的值可以在通道中找到
String a = "......"; //实际要发送的JSON
JSONObject json = JSONObject.parseObject(a);
System.out.println("JSON string = " + json.toString());
System.out.println(httpClientWithBasicAuth(kuser, kpass, url, json));
————————————————
运行结果
response.getStatusLine():HTTP/1.1 200 OK
Http status:200
Key:server ,Value : SAP NetWeaver Application Server 7.49 / AS Java 7.50
Key:date ,Value : Fri, 09 Jul 2021 02:15:44 GMT
Key:content-type ,Value : application/json; charset=UTF-8
Key:set-cookie ,Value : saplb_*=(J2EE1265820)1265850; Version=1; Path=/
Key:set-cookie ,Value : JSESSIONID=gvf8tv92nZTiKlbEo0R8kP9voAyJegG6UBMA_SAPRTgFEfSYnXZ-W5mogIkB44CP; Version=1; Path=/
[server: SAP NetWeaver Application Server 7.49 / AS Java 7.50, date: Fri, 09 Jul 2021 02:15:44 GMT, content-type: application/json; charset=UTF-8, set-cookie: saplb_*=(J2EE1265820)1265850; Version=1; Path=/, set-cookie: JSESSIONID=gvf8tv92nZTiKlbEo0R8kP9voAyJegG6UBMA_SAPRTgFEfSYnXZ-W5mogIkB44CP; Version=1; Path=/]
——————————————
以上是关于Java 调用SAP PO 的Rest接口的主要内容,如果未能解决你的问题,请参考以下文章