Skip to content

Commit d8d715e

Browse files
committed
Add content type support to JSON async entity producers
Allow AbstractJsonEntityProducer and JsonObjectEntityProducer to use an explicit ContentType for streamed JSON request bodies.
1 parent e2b5268 commit d8d715e

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/AbstractJsonEntityProducer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@
3737
abstract class AbstractJsonEntityProducer implements AsyncEntityProducer {
3838

3939
private final InternalBuffer buffer;
40+
private final ContentType contentType;
4041
private volatile State state;
4142

4243
AbstractJsonEntityProducer(final int initSize) {
44+
this(initSize, null);
45+
}
46+
47+
AbstractJsonEntityProducer(final int initSize, final ContentType contentType) {
4348
this.buffer = new InternalBuffer(initSize);
49+
this.contentType = contentType != null ? contentType : ContentType.APPLICATION_JSON;
4450
this.state = State.ACTIVE;
4551
}
4652

@@ -58,7 +64,7 @@ public final Set<String> getTrailerNames() {
5864

5965
@Override
6066
public final String getContentType() {
61-
return ContentType.APPLICATION_JSON.toString();
67+
return contentType.toString();
6268
}
6369

6470
@Override

httpcore5-jackson2/src/main/java/org/apache/hc/core5/jackson2/http/JsonObjectEntityProducer.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import com.fasterxml.jackson.databind.ObjectMapper;
3333

34+
import org.apache.hc.core5.http.ContentType;
3435
import org.apache.hc.core5.util.Args;
3536

3637
/**
@@ -45,12 +46,25 @@ public class JsonObjectEntityProducer<T> extends AbstractJsonEntityProducer {
4546
private final T jsonObject;
4647
private final ObjectMapper objectMapper;
4748

48-
public JsonObjectEntityProducer(final T jsonObject, final ObjectMapper objectMapper) {
49-
super(4096);
49+
/**
50+
* Creates a new instance that serializes the given object using the specified content type.
51+
* If {@code contentType} is {@code null}, defaults to {@code application/json}.
52+
*
53+
* @param jsonObject the object to serialize.
54+
* @param objectMapper the object mapper.
55+
* @param contentType the content type, or {@code null} for {@code application/json}.
56+
* @since 5.5
57+
*/
58+
public JsonObjectEntityProducer(final T jsonObject, final ObjectMapper objectMapper, final ContentType contentType) {
59+
super(4096, contentType);
5060
this.jsonObject = Args.notNull(jsonObject, "Json object");
5161
this.objectMapper = Args.notNull(objectMapper, "Object mapper");
5262
}
5363

64+
public JsonObjectEntityProducer(final T jsonObject, final ObjectMapper objectMapper) {
65+
this(jsonObject, objectMapper, null);
66+
}
67+
5468
@Override
5569
final void generateJson(final OutputStream outputStream) throws IOException {
5670
objectMapper.writeValue(outputStream, jsonObject);

0 commit comments

Comments
 (0)