use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.
the class ForestRequest method addJSONQuery.
/**
* 添加 JSON Query 参数
*
* @param name Query参数名
* @param value Query参数值
* @return {@link ForestRequest}对象实例
* @since 1.5.4
*/
public ForestRequest<T> addJSONQuery(String name, Object value) {
ForestJsonConverter jsonConverter = configuration.getJsonConverter();
String json = jsonConverter.encodeToString(value);
this.query.addQuery(name, json);
return this;
}
use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.
the class JSONFilter method doFilter.
@Override
public Object doFilter(ForestConfiguration configuration, Object data) {
ForestJsonConverter jsonConverter = configuration.getJsonConverter();
String json = jsonConverter.encodeToString(data);
return json;
}
use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.
the class ForestBody method nameValuesMapWithObject.
/**
* 获取请求体中的键值对
* <p>包含{@link NameValueRequestBody}类型请求体项、以及{@link ObjectRequestBody}类请求体项拆解出来的键值对数据
*
* @return
*/
public Map<String, Object> nameValuesMapWithObject() {
Map<String, Object> map = new LinkedHashMap<>();
ForestJsonConverter jsonConverter = configuration.getJsonConverter();
for (ForestRequestBody body : bodyItems) {
if (body instanceof NameValueRequestBody) {
NameValueRequestBody nameValueRequestBody = (NameValueRequestBody) body;
String name = nameValueRequestBody.getName();
if (!map.containsKey(name)) {
map.put(name, nameValueRequestBody.getValue());
}
} else if (body instanceof ObjectRequestBody) {
ObjectRequestBody objectRequestBody = (ObjectRequestBody) body;
Map<String, Object> keyValueMap = jsonConverter.convertObjectToMap(objectRequestBody.getObject());
for (Map.Entry<String, Object> entry : keyValueMap.entrySet()) {
String name = entry.getKey();
Object value = entry.getValue();
if (!map.containsKey(name)) {
map.put(name, value);
}
}
}
}
return map;
}
use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.
the class ObjectRequestBody method getNameValueList.
@Override
public List<RequestNameValue> getNameValueList(ForestConfiguration configuration) {
List<RequestNameValue> nameValueList = new LinkedList<>();
ForestJsonConverter jsonConverter = configuration.getJsonConverter();
Map<String, Object> map = jsonConverter.convertObjectToMap(object);
for (Map.Entry<String, Object> entry : map.entrySet()) {
nameValueList.add(new RequestNameValue(entry.getKey(), entry.getValue(), MappingParameter.TARGET_BODY));
}
return nameValueList;
}
use of com.dtflys.forest.converter.json.ForestJsonConverter in project forest by dromara.
the class OkHttp3BodyBuilder method setFileBody.
@Override
protected void setFileBody(Request.Builder builder, ForestRequest request, Charset charset, String contentType, LifeCycleHandler lifeCycleHandler) {
String boundary = request.getBoundary();
MultipartBody.Builder bodyBuilder = null;
if (StringUtils.isNotEmpty(boundary)) {
bodyBuilder = new MultipartBody.Builder(boundary);
} else {
bodyBuilder = new MultipartBody.Builder();
}
ContentType objContentType = new ContentType(contentType);
MediaType mediaType = MediaType.parse(objContentType.toStringWithoutParameters());
if ("multipart".equals(mediaType.type())) {
bodyBuilder.setType(mediaType);
}
ForestJsonConverter jsonConverter = request.getConfiguration().getJsonConverter();
List<ForestMultipart> multiparts = request.getMultiparts();
for (ForestRequestBody item : request.body()) {
if (item instanceof NameValueRequestBody) {
NameValueRequestBody nameValueItem = (NameValueRequestBody) item;
String name = nameValueItem.getName();
Object value = nameValueItem.getValue();
String partContentType = nameValueItem.getContentType();
addMultipart(bodyBuilder, name, value, partContentType, charset, jsonConverter);
} else if (item instanceof ObjectRequestBody) {
Object obj = ((ObjectRequestBody) item).getObject();
if (obj == null) {
continue;
}
Map<String, Object> attrs = jsonConverter.convertObjectToMap(obj);
for (Map.Entry<String, Object> entry : attrs.entrySet()) {
String name = entry.getKey();
Object value = entry.getValue();
addMultipart(bodyBuilder, name, value, null, charset, jsonConverter);
}
}
}
for (ForestMultipart multipart : multiparts) {
RequestBody fileBody = createFileBody(request, multipart, charset, lifeCycleHandler);
bodyBuilder.addFormDataPart(multipart.getName(), multipart.getOriginalFileName(), fileBody);
}
MultipartBody body = bodyBuilder.build();
builder.method(request.getType().getName(), body);
}
Aggregations