Skip to content

Commit

Permalink
release 2.1.1 version
Browse files Browse the repository at this point in the history
  • Loading branch information
杨一点 committed Aug 17, 2020
1 parent 54d4dcc commit e28250e
Show file tree
Hide file tree
Showing 35 changed files with 663 additions and 339 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
- 支持字节跳动(抖音,头条..)开放平台小程序代开发功能的后端开发。
部分代码参考了[WxJava](https://github.com/Wechat-Group/WxJava)的实现,使用方式也大同小异。
- 支持`JoddHttp`, `OkHttpClient`, `RestTemplate`
- 支持`Gson`, `Jackson`, `FastJson`
- 提供Docker版本, 填写好第三方配置后, 通过`docker-compose`一键启动所有所需服务
- 支持`Gson`, `Jackson`, `FastJson`(不推荐使用FastJson,[原因](https://segmentfault.com/a/1190000015634321))
## 使用方式

#### Maven 引用
Expand All @@ -24,4 +23,5 @@
<version>2.1.0.SNAPSHOT</version>
</dependency>
```
具体可以参考[ByteDance-Open-Demo](https://github.com/yydzxz/ByteDance-Open-Demo)
具体可以参考[ByteDance-Open-Demo](https://github.com/yydzxz/ByteDance-Open-Demo)
- Demo提供了Docker版本, 填写好第三方配置后, 通过`docker-compose`一键启动所有所需服务
17 changes: 10 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.yydzxz</groupId>
<artifactId>bytedance-open</artifactId>
<version>2.1.0.SNAPSHOT</version>
<version>2.1.1.RELEASE</version>

<name>ByteDance - Open Java SDK</name>
<description>字节跳动开放平台Java SDK</description>
Expand Down Expand Up @@ -57,11 +57,6 @@
<artifactId>redisson</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand All @@ -78,7 +73,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.67</version>
<version>1.2.73</version>
<scope>provided</scope>
</dependency>
<!-- json序列化工具类 end-->
Expand Down Expand Up @@ -118,8 +113,16 @@
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.5.0</version>
<scope>test</scope>
</dependency>
<!-- 单元测试 end-->


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.yydzxz.common;
package com.github.yydzxz.common.message;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.yydzxz.common.redis.IByteDanceRedisOps;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;

/**
* @author yangyidian
Expand All @@ -19,7 +19,7 @@ public ByteDanceMessageInRedisDuplicateChecker(IByteDanceRedisOps byteDanceRedis

@Override
public boolean isDuplicate(String messageId) {
boolean notExist = StringUtils.isEmpty(byteDanceRedisOps.getValue(messageId));
boolean notExist = StrUtil.isEmpty(byteDanceRedisOps.getValue(messageId));
if(notExist){
byteDanceRedisOps.setValue(messageId, DateUtil.now(), 6, TimeUnit.MINUTES);
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.yydzxz.common;
package com.github.yydzxz.common.message;

/**
* 消息重复检查器。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.yydzxz.common.service.IByteDanceResponse;
import com.github.yydzxz.common.util.json.JsonSerializer;
import com.google.common.collect.Multimap;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -17,12 +18,12 @@
@Slf4j
public abstract class AbstractByteDanceHttpRequestService implements IByteDanceHttpRequestService {

private JsonSerializer jsonSerializer;

public AbstractByteDanceHttpRequestService(JsonSerializer jsonSerializer) {
this.jsonSerializer = jsonSerializer;
}

private JsonSerializer jsonSerializer;

@Override
public JsonSerializer getJsonSerializer() {
return jsonSerializer;
Expand All @@ -48,7 +49,6 @@ public <T> T post(String url, Object request, Class<T> t){
log.info("请求字节跳动接口返回数据: 【{}】", getJsonSerializer().toJson(response));
}else {
log.info("请求字节跳动接口返回数据, 类型:【{}】, 内容:【{}】", t.getTypeName(), response);

}
return handleResponse(response);
}
Expand All @@ -61,7 +61,6 @@ public <T> T postWithHeaders(String url, Multimap<String, String> headers, Objec
log.info("请求字节跳动接口返回数据: 【{}】", getJsonSerializer().toJson(response));
}else {
log.info("请求字节跳动接口返回数据, 类型:【{}】, 内容:【{}】", t.getTypeName(), response);

}
return handleResponse(response);
}
Expand Down Expand Up @@ -108,11 +107,26 @@ String doPost(String url, Object obj){
abstract <T> T doPostWithHeaders(String url, Multimap<String, String> headers, Object requestParam, Class<T> t);

/**
* 有些值要特殊处理,比如用Resttemplate的时候,File要转成FileSystemResource
* 有些参数的值要特殊处理,比如用Resttemplate上传文件时,File要转成FileSystemResource
* @param requestParams
* @return
*/
abstract Object handlerRequestParam(Object requestParams);
Object handlerRequestParam(Object requestParams){
Field[] fields = requestParams.getClass().getDeclaredFields();
Map<String, Object> paramsMap = new HashMap<>(fields.length);
for(Field field : fields){
field.setAccessible(true);
Object value;
try {
value = field.get(requestParams);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
String aliasName = getJsonSerializer().getFieldAliasName(field);
paramsMap.put(aliasName, value);
}
return paramsMap;
}

Map<String, String> multimapHeaders2MapHeaders(Multimap<String, String> headers){
Map<String,String> headerMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,4 @@ <T> T doPostWithHeaders(String url, Multimap<String, String> headers, Object req
return getJsonSerializer().parse(response.bodyText(), clazz);
}
}

@Override
Object handlerRequestParam(Object requestParams) {
Map<String, Object> paramsMap = new HashMap<>();
Field[] fields = requestParams.getClass().getDeclaredFields();
for(Field field : fields){
field.setAccessible(true);
Object value;
try {
value = field.get(requestParams);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
String aliasName = getJsonSerializer().getFieldAliasName(field);
paramsMap.put(aliasName, value);
}
return paramsMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,6 @@ public <T> T doPostWithHeaders(String url, Multimap<String, String> headers, Obj
return doRequest(request, clazz);
}

@Override
Object handlerRequestParam(Object requestParams) {
Field[] fields = requestParams.getClass().getDeclaredFields();
Map<String, Object> paramsMap = new HashMap<>(fields.length);
for(Field field : fields){
field.setAccessible(true);
Object value;
try {
value = field.get(requestParams);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
String aliasName = getJsonSerializer().getFieldAliasName(field);
paramsMap.put(aliasName, value);
}
return paramsMap;
}

@Override
public <T> T doPost(String url, Object requestParam, Class<T> clazz) {
RequestBody body = RequestBody.create(getJsonSerializer().toJson(requestParam), JSON);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.yydzxz.common.util.json;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.annotation.JSONField;
Expand All @@ -8,9 +9,12 @@
import java.lang.reflect.Field;

/**
* 问题太多,不推荐使用
* https://segmentfault.com/a/1190000015634321
* @author yangyidian
* @date 2020/08/03
**/
@Deprecated
public class FastJsonSerializer implements JsonSerializer {
private static final SerializeConfig config = new SerializeConfig();
static {
Expand All @@ -29,12 +33,14 @@ public <T> T parse(String jsonString, Class<T> clazz){

@Override
public String getFieldAliasName(Field field) {
String fieldAliasName = field.getName();
Annotation annotation = field.getAnnotation(JSONField.class);
if(annotation != null){
JSONField serializedNameAnnotation = (JSONField)annotation;
return serializedNameAnnotation.name();
}else {
return field.getName();
if(!StrUtil.isEmpty(serializedNameAnnotation.name())){
fieldAliasName = serializedNameAnnotation.name();
}
}
return fieldAliasName;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.yydzxz.common.util.json;

import cn.hutool.core.util.StrUtil;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -29,13 +30,15 @@ public <T> T parse(String jsonString, Class<T> clazz) {

@Override
public String getFieldAliasName(Field field){
String fieldAliasName = field.getName();
Annotation annotation = field.getAnnotation(SerializedName.class);
if(annotation != null){
SerializedName serializedNameAnnotation = (SerializedName)annotation;
return serializedNameAnnotation.value();
}else {
return field.getName();
if(!StrUtil.isEmpty(serializedNameAnnotation.value())){
fieldAliasName = serializedNameAnnotation.value();
}
}
return fieldAliasName;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.yydzxz.common.util.json;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -25,7 +26,7 @@ public class JacksonSerializer implements JsonSerializer {
public String toJson(Object object) {
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String jsonStr = null;
String jsonStr;
try {
jsonStr = mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
Expand All @@ -48,18 +49,22 @@ public <T> T parse(String jsonString, Class<T> clazz) {

@Override
public String getFieldAliasName(Field field) {
String fieldAliasName = field.getName();
Annotation annotation = field.getAnnotation(JsonAlias.class);
if(annotation != null){
JsonAlias jsonAnnotation = (JsonAlias)annotation;
return jsonAnnotation.value()[0];
if(jsonAnnotation.value().length > 0 && !StrUtil.isEmpty(jsonAnnotation.value()[0])){
fieldAliasName = jsonAnnotation.value()[0];
}
}else {
annotation = field.getAnnotation(JsonProperty.class);
if(annotation != null){
JsonProperty jsonAnnotation = (JsonProperty)annotation;
return jsonAnnotation.value();
}else {
return field.getName();
if(!StrUtil.isEmpty(jsonAnnotation.value())){
fieldAliasName = jsonAnnotation.value();
}
}
}
return fieldAliasName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@
**/
public interface JsonSerializer {

/**
* 对象转json字符串
* @param object
* @return
*/
String toJson(Object object);

/**
* json字符串转对象
* @param jsonString
* @param clazz
* @param <T>
* @return
*/
<T> T parse(String jsonString, Class<T> clazz);

/**
Expand Down
Loading

0 comments on commit e28250e

Please sign in to comment.