JSON.filter(..) 추가, WebClient.bodyData() 수정

master
mjkhan21 1 year ago
parent f35b0ba597
commit 7d1c4120c8

@ -1,9 +1,15 @@
package cokr.xit.foundation.data;
import java.io.InputStream;
import java.util.Set;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import cokr.xit.foundation.AbstractComponent;
@ -12,6 +18,7 @@ import cokr.xit.foundation.AbstractComponent;
*/
public class JSON extends AbstractComponent {
private ObjectMapper objectMapper;
private FilterProvider filterProvider;
/**JSON / ObjectMapper .
* @return JSON / ObjectMapper
@ -20,6 +27,7 @@ public class JSON extends AbstractComponent {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
objectMapper.configure(Feature.ALLOW_COMMENTS, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
return objectMapper;
}
@ -33,6 +41,15 @@ public class JSON extends AbstractComponent {
return this;
}
/** JSON Filter .<br />
* {@code @JsonFilter("필터 아이디")} .
* @param id
* @return Filter
*/
public Filter filter(String id) {
return new Filter().init(id, this);
}
/**JSON .
* @param <T>
* @param json JSON
@ -80,10 +97,81 @@ public class JSON extends AbstractComponent {
public String stringify(Object obj, boolean indent) {
if (obj instanceof String)
return (String)obj;
ObjectMapper mapper = getObjectMapper();
ObjectWriter writer = null;
if (!indent) {
writer = filterProvider == null ? mapper.writer() : mapper.writer(filterProvider);
} else {
writer = mapper.writerWithDefaultPrettyPrinter();
if (filterProvider != null)
writer = writer.with(filterProvider);
}
try {
return getObjectMapper().writeValueAsString(obj);
return writer.writeValueAsString(obj);
} catch (Exception e) {
throw runtimeException(e);
}
}
/** JSON .<br />
* {@code @JsonFilter("필터 아이디")} .
* @author mjkhan
*/
public static class Filter {
private String id;
private JSON json;
private Filter init(String id, JSON json) {
this.id = id;
this.json = json;
return this;
}
/**JSON .<br />
* .
* @param properties JSON
* @return JSON
*/
public JSON include(Set<String> properties) {
return setProvider(!isEmpty(properties) ?
SimpleBeanPropertyFilter.filterOutAllExcept(properties) :
SimpleBeanPropertyFilter.serializeAll()
);
}
/**JSON .<br />
* .
* @param properties JSON
* @return JSON
*/
public JSON include(String... properties) {
return include(Set.of(properties));
}
/**JSON .
* @param properties JSON
* @return JSON
*/
public JSON exclude(Set<String> properties) {
notEmpty(properties, "properties");
return setProvider(SimpleBeanPropertyFilter.serializeAllExcept(properties));
}
/**JSON .
* @param properties JSON
* @return JSON
*/
public JSON exclude(String... properties) {
return exclude(Set.of(properties));
}
private JSON setProvider(SimpleBeanPropertyFilter filter) {
SimpleFilterProvider provider = new SimpleFilterProvider();
provider.addFilter(id, filter);
json.filterProvider = provider;
return json;
}
}
}

@ -26,6 +26,8 @@ import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import javax.net.ssl.SSLContext;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.data.JSON;
@ -83,6 +85,7 @@ public class WebClient {
private HttpClient.Version version = HttpClient.Version.HTTP_2;
private Duration timeout = Duration.ofSeconds(30);
private Authenticator authenticator;
private SSLContext sslContext;
private ProxySelector proxy;
private HttpClient.Redirect followRedirects = HttpClient.Redirect.NORMAL;
@ -114,6 +117,15 @@ public class WebClient {
return this;
}
/**SSL SSLContext .
* @param sslContext SSLContext
* @return WebClient
*/
public WebClient sslContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}
/** proxy .
* @param proxy
* @return WebClient
@ -140,6 +152,8 @@ public class WebClient {
if (authenticator != null)
builder.authenticator(authenticator);
if (sslContext != null)
builder.sslContext(sslContext);
if (proxy != null)
builder.proxy(proxy);
@ -241,6 +255,9 @@ public class WebClient {
* @author mjkhan
*/
public static class Request {
/** 요청 성공 */
public static final int SUCCESS = 200;
public static enum ContentType {
FORM("application/x-www-form-urlencoded"),
JSON("application/json"),
@ -272,6 +289,8 @@ public class WebClient {
private Charset charset = StandardCharsets.UTF_8;
private LinkedHashMap<String, String> headers;
private LinkedHashMap<String, Object> keyValues;
private JSON json;
private Consumer<HttpResponse<String>> textHandler = hresp -> {
HttpHeaders headers = hresp.headers();
headers.map().forEach((k, v) -> System.out.println(k + " = " + v));
@ -340,17 +359,18 @@ public class WebClient {
return this;
}
/** JSON . false.
* @param json JSON
* <ul><li> JSON true</li>
* <li> false</li>
* </ul>
/** JSON JSON .
* @param json JSON
* @return Request
public Request json(boolean json) {
*/
public Request json(JSON json) {
this.json = json;
return this;
return contentType(ContentType.JSON);
}
private JSON json() {
return Assert.ifEmpty(json, () -> json = new JSON());
}
*/
/** . false().
* @param download
@ -365,7 +385,8 @@ public class WebClient {
}
private Object bodyData() {
if (Assert.isEmpty(keyValues)) return null;
int size = keyValues != null ? keyValues.size() : 0;
if (size != 1) return null;
Object value = keyValues.remove("body");
if (value != null)
@ -448,10 +469,10 @@ public class WebClient {
private String inJSON() {
Object body = bodyData();
if (!Assert.isEmpty(body))
return new JSON().stringify(body);
return json().stringify(body);
if (!Assert.isEmpty(keyValues))
return new JSON().stringify(keyValues);
return json().stringify(keyValues);
return "";
}

Loading…
Cancel
Save