从sap获取数据写入数据库操作
一、什么是从sap获取数据?
每个公司拉sap数据方法不一样,有些还需要验证,具体问题,具体分析,这边大致给一个思路
SAP,为“System Applications and Products”的简称,是SAP公司的产品——企业管理解决方案的软件名称。SAP公司(纽交所代码:SAP)成立于1972年。
简而言之就是写一个接口,让别人调用,你发送一个json格式数据过去,对面响应并且返回数据,再写入数据库
二、如何是从sap获取数据?
这种情况下,数据库是不会优先建立的,需要你先得到数据,再去写数据库,最后插入数据
1.先在controller层写出接口
@Slf4j
@RestController
@RequestMapping("接口")
public class TabZmmo3110MaterialDataController {
@PostMapping("地址")
@ResponseBody
public void getHistoryBill(@RequestBody String object) {
System.out.print(object);
log.info("------------call------------");
}
}
2.在call层写出方法(call层为请求层)
@RestController
@RequestMapping("接口")
@Slf4j
public class Zmmo3110MaterialDataCall {
@GetMapping("/地址")
public int execute() throws Exception {
log.info("call---------------");
Map<String, Object> params = new HashMap<String, Object>();
Map<String, Object> p = new HashMap<String, Object>();
p.put("abcde", "122");
p.put("abcdef", "121");
p.put("abcdefg", "222");
p.put("abcdefgh", "111");
p.put("a", "211");
p.put("ab", "2121");
p.put("abc", "1212");
params.put("", p);
String url = "这边写你sap的地址"; //测试
int code = run(params, url);
if (code == 202 || code == 200 || code == 204) {
log.info("请求成功!");
return code;
} else {
log.info("程序异常!");
return code;
}
}
public int run(Map<String, Object> params, String url) {
Gson gson = new Gson();
String pars = gson.toJson(params);
String userName = "test";
String password = "123456";
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, password));
CloseableHttpClient createDefault = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(pars.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);
CloseableHttpResponse result = null;
try {
result = createDefault.execute(post);
} catch (IOException e) {
e.printStackTrace();
}
int code = result.getStatusLine().getStatusCode();
return code;
}
}
(1)Gson工具包
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.google.gson;
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.Excluder;
import com.google.gson.internal.Primitives;
import com.google.gson.internal.Streams;
import com.google.gson.internal.bind.ArrayTypeAdapter;
import com.google.gson.internal.bind.CollectionTypeAdapterFactory;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory;
import com.google.gson.internal.bind.JsonTreeReader;
import com.google.gson.internal.bind.JsonTreeWriter;
import com.google.gson.internal.bind.MapTypeAdapterFactory;
import com.google.gson.internal.bind.ObjectTypeAdapter;
import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory;
import com.google.gson.internal.bind.SqlDateTypeAdapter;
import com.google.gson.internal.bind.TimeTypeAdapter;
import com.google.gson.internal.bind.TypeAdapters;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.google.gson.stream.MalformedJsonException;
import java.io.EOFException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;
public final class Gson {
static final boolean DEFAULT_JSON_NON_EXECUTABLE = false;
static final boolean DEFAULT_LENIENT = false;
static final boolean DEFAULT_PRETTY_PRINT = false;
static final boolean DEFAULT_ESCAPE_HTML = true;
static final boolean DEFAULT_SERIALIZE_NULLS = false;
static final boolean DEFAULT_COMPLEX_MAP_KEYS = false;
static final boolean DEFAULT_SPECIALIZE_FLOAT_VALUES = false;
private static final TypeToken<?> NULL_KEY_SURROGATE = TypeToken.get(Object.class);
private static final String JSON_NON_EXECUTABLE_PREFIX = ")]}'\n";
private final ThreadLocal<Map<TypeToken<?>, Gson.FutureTypeAdapter<?>>> calls;
private final Map<TypeToken<?>, TypeAdapter<?>> typeTokenCache;
private final ConstructorConstructor constructorConstructor;
private final JsonAdapterAnnotationTypeAdapterFactory jsonAdapterFactory;
final List<TypeAdapterFactory> factories;
final Excluder excluder;
final FieldNamingStrategy fieldNamingStrategy;
final Map<Type, InstanceCreator<?>> instanceCreators;
final boolean serializeNulls;
final boolean complexMapKeySerialization;
final boolean generateNonExecutableJson;
final boolean htmlSafe;
final boolean prettyPrinting;
final boolean lenient;
final boolean serializeSpecialFloatingPointValues;
final String datePattern;
final int dateStyle;
final int timeStyle;
final LongSerializationPolicy longSerializationPolicy;
final List<TypeAdapterFactory> builderFactories;
final List<TypeAdapterFactory> builderHierarchyFactories;
public Gson() {
this(Excluder.DEFAULT, FieldNamingPolicy.IDENTITY, Collections.emptyMap(), false, false, false, true, false, false, false, LongSerializationPolicy.DEFAULT, (String)null, 2, 2, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
}
Gson(Excluder excluder, FieldNamingStrategy fieldNamingStrategy, Map<Type, InstanceCreator<?>> instanceCreators, boolean serializeNulls, boolean complexMapKeySerialization, boolean generateNonExecutableGson, boolean htmlSafe, boolean prettyPrinting, boolean lenient, boolean serializeSpecialFloatingPointValues, LongSerializationPolicy longSerializationPolicy, String datePattern, int dateStyle, int timeStyle, List<TypeAdapterFactory> builderFactories, List<TypeAdapterFactory> builderHierarchyFactories, List<TypeAdapterFactory> factoriesToBeAdded) {
this.calls = new ThreadLocal();
this.typeTokenCache = new ConcurrentHashMap();
this.excluder = excluder;
this.fieldNamingStrategy = fieldNamingStrategy;
this.instanceCreators = instanceCreators;
this.constructorConstructor = new ConstructorConstructor(instanceCreators);
this.serializeNulls = serializeNulls;
this.complexMapKeySerialization = complexMapKeySerialization;
this.generateNonExecutableJson = generateNonExecutableGson;
this.htmlSafe = htmlSafe;
this.prettyPrinting = prettyPrinting;
this.lenient = lenient;
this.serializeSpecialFloatingPointValues = serializeSpecialFloatingPointValues;
this.longSerializationP