Skip to content

Commit

Permalink
Jackson Encoder/Decoder accept custom MimeType's
Browse files Browse the repository at this point in the history
Issue: SPR-15474
  • Loading branch information
rstoyanchev committed May 4, 2017
1 parent afa1c93 commit 8231812
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Expand Up @@ -35,6 +35,7 @@
import org.springframework.core.ResolvableType;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import org.springframework.util.ObjectUtils;

/**
* Base class providing support methods for Jackson 2.9 encoding and decoding.
Expand Down Expand Up @@ -62,19 +63,21 @@ public abstract class Jackson2CodecSupport {

protected final ObjectMapper objectMapper;

private final List<MimeType> mimeTypes;


/**
* Constructor with a Jackson {@link ObjectMapper} to use.
*/
protected Jackson2CodecSupport(ObjectMapper objectMapper) {
protected Jackson2CodecSupport(ObjectMapper objectMapper, MimeType... mimeTypes) {
Assert.notNull(objectMapper, "ObjectMapper must not be null");
this.objectMapper = objectMapper;
this.mimeTypes = !ObjectUtils.isEmpty(mimeTypes) ? Arrays.asList(mimeTypes) : JSON_MIME_TYPES;
}


protected boolean supportsMimeType(MimeType mimeType) {
return (mimeType == null ||
JSON_MIME_TYPES.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
return (mimeType == null || this.mimeTypes.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
}

protected JavaType getJavaType(Type type, Class<?> contextClass) {
Expand Down
Expand Up @@ -61,8 +61,8 @@ public Jackson2JsonDecoder() {
super(Jackson2ObjectMapperBuilder.json().build());
}

public Jackson2JsonDecoder(ObjectMapper mapper) {
super(mapper);
public Jackson2JsonDecoder(ObjectMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes);
}


Expand Down
Expand Up @@ -71,8 +71,8 @@ public Jackson2JsonEncoder() {
this(Jackson2ObjectMapperBuilder.json().build());
}

public Jackson2JsonEncoder(ObjectMapper mapper) {
super(mapper);
public Jackson2JsonEncoder(ObjectMapper mapper, MimeType... mimeTypes) {
super(mapper, mimeTypes);
this.streamingMediaTypes.add(MediaType.APPLICATION_STREAM_JSON);
this.ssePrettyPrinter = initSsePrettyPrinter();
}
Expand Down

0 comments on commit 8231812

Please sign in to comment.