You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ab...@apache.org on 2026/04/12 12:20:50 UTC

(httpcomponents-core) branch master updated: Add content type support to JSON async entity producers

This is an automated email from the ASF dual-hosted git repository.

abernal pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 54135f6ca Add content type support to JSON async entity producers
54135f6ca is described below

commit 54135f6cacfeb28208c58b4dadd355f6b5a2dfad
Author: Arturo Bernal <ab...@apache.org>
AuthorDate: Sat Apr 11 13:46:19 2026 +0200

    Add content type support to JSON async entity producers
    
    Allow AbstractJsonEntityProducer and JsonObjectEntityProducer to use an
    explicit ContentType for streamed JSON request bodies.
---
 .../jackson2/http/AbstractJsonEntityProducer.java      |  8 +++++++-
 .../core5/jackson2/http/JsonObjectEntityProducer.java  | 18 ++++++++++++++++--
 2 files changed, 23 insertions(+), 3 deletions(-)

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<String> 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<T> 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);