diff --git a/httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/AbstractJsonEntityProducer.java b/httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/AbstractJsonEntityProducer.java index d1b02ded8..3125011be 100644 --- a/httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/AbstractJsonEntityProducer.java +++ b/httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/AbstractJsonEntityProducer.java @@ -37,10 +37,16 @@ abstract class AbstractJsonEntityProducer implements AsyncEntityProducer { private final InternalBuffer buffer; + private final ContentType contentType; private volatile State state; AbstractJsonEntityProducer(final int initSize) { + this(initSize, null); + } + + AbstractJsonEntityProducer(final int initSize, final ContentType contentType) { this.buffer = new InternalBuffer(initSize); + this.contentType = contentType != null ? contentType : ContentType.APPLICATION_JSON; this.state = State.ACTIVE; } @@ -58,7 +64,7 @@ public final Set getTrailerNames() { @Override public final String getContentType() { - return ContentType.APPLICATION_JSON.toString(); + return contentType.toString(); } @Override diff --git a/httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/JsonObjectEntityProducer.java b/httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/JsonObjectEntityProducer.java index 3a6bd71f4..6093fae79 100644 --- a/httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/JsonObjectEntityProducer.java +++ b/httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/JsonObjectEntityProducer.java @@ -31,6 +31,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.util.Args; /** @@ -45,12 +46,25 @@ public class JsonObjectEntityProducer extends AbstractJsonEntityProducer { private final T jsonObject; private final ObjectMapper objectMapper; - public JsonObjectEntityProducer(final T jsonObject, final ObjectMapper objectMapper) { - super(4096); + /** + * Creates a new instance that serializes the given object using the specified content type. + * If {@code contentType} is {@code null}, defaults to {@code application/json}. + * + * @param jsonObject the object to serialize. + * @param objectMapper the object mapper. + * @param contentType the content type, or {@code null} for {@code application/json}. + * @since 5.5 + */ + public JsonObjectEntityProducer(final T jsonObject, final ObjectMapper objectMapper, final ContentType contentType) { + super(4096, contentType); this.jsonObject = Args.notNull(jsonObject, "Json object"); this.objectMapper = Args.notNull(objectMapper, "Object mapper"); } + public JsonObjectEntityProducer(final T jsonObject, final ObjectMapper objectMapper) { + this(jsonObject, objectMapper, null); + } + @Override final void generateJson(final OutputStream outputStream) throws IOException { objectMapper.writeValue(outputStream, jsonObject);