Search in sources :

Example 6 with ForestJsonConverter

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;
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter)

Example 7 with ForestJsonConverter

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;
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter)

Example 8 with ForestJsonConverter

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;
}
Also used : NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody) ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with ForestJsonConverter

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;
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) RequestNameValue(com.dtflys.forest.utils.RequestNameValue) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 10 with ForestJsonConverter

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);
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) ContentType(com.dtflys.forest.backend.ContentType) ForestMultipart(com.dtflys.forest.multipart.ForestMultipart) ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody) NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody) Map(java.util.Map) ForestRequestBody(com.dtflys.forest.http.ForestRequestBody) ObjectRequestBody(com.dtflys.forest.http.body.ObjectRequestBody) NameValueRequestBody(com.dtflys.forest.http.body.NameValueRequestBody)

Aggregations

ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)20 RequestNameValue (com.dtflys.forest.utils.RequestNameValue)6 Map (java.util.Map)4 Test (org.junit.Test)4 ForestGsonConverter (com.dtflys.forest.converter.json.ForestGsonConverter)3 ForestQueryParameter (com.dtflys.forest.http.ForestQueryParameter)3 ForestRequestBody (com.dtflys.forest.http.ForestRequestBody)3 NameValueRequestBody (com.dtflys.forest.http.body.NameValueRequestBody)3 ObjectRequestBody (com.dtflys.forest.http.body.ObjectRequestBody)3 ForestMultipart (com.dtflys.forest.multipart.ForestMultipart)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)2 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)2 ForestVariableUndefinedException (com.dtflys.forest.exceptions.ForestVariableUndefinedException)2 ForestURL (com.dtflys.forest.http.ForestURL)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 ContentType (com.dtflys.forest.backend.ContentType)1 AddressSource (com.dtflys.forest.callback.AddressSource)1 OnError (com.dtflys.forest.callback.OnError)1