eclipse-ee4j / eclipse-ee4j/jersey

ContextResolver<ObjectMapper> don't work when register a MessageBodyReader<Object>

Open
#3,236 3 comments 0 reactions 0 assignees View on GitHub
Component: core evaluation-needed Priority: Major Type: Bug
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

### I had this configuration working fine :

```
this.register(new JacksonObjectMapperProvider());
this.register(JacksonFeature.class);
```
```
public class JacksonObjectMapperProvider
implements ContextResolver {

public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";

final ObjectMapper objectMapper;

public JacksonObjectMapperProvider(final DateFormat dateFormat) {

Objects.requireNonNull(dateFormat, "dateFormat must be not null.");
this.objectMapper = new ObjectMapper();
this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
this.objectMapper.setSerializationInclusion(Include.NON_NULL);
this.objectMapper.setDateFormat(dateFormat);
}

public JacksonObjectMapperProvider() {

this(new SimpleDateFormat(DEFAULT_DATE_FORMAT));
}

public JacksonObjectMapperProvider(final String dateFormatPattern) {

this(new SimpleDateFormat(dateFormatPattern));
}

public JacksonObjectMapperProvider(final ObjectMapper objectMapper) {

Objects.requireNonNull(objectMapper, "provided objectMapper must be not null.");
this.objectMapper = objectMapper;
}

@Override
public ObjectMapper getContext(
@SuppressWarnings("unused") final Class type) {

return this.objectMapper;
}

public ObjectMapper getObjectMapper() {

return this.objectMapper;
}
}
```

### But when I wanted to add my own implementation of MessageBodyReader by registering this class (the Object mapper reference is not the same at runtime) :

```
this.register(new JacksonObjectMapperProvider());
this.register(JacksonFeature.class);
this.register(CustomMessageBodyReader.class);
```
```
@Provider
@Priority(Priorities.ENTITY_CODER)
public class CustomMessageBodyReader
implements MessageBodyReader {

private final JacksonJsonProvider delegate;

private final Validator validator;

@Inject
public ValidationIntercepter(final JacksonJsonProvider delegate, final Validator validator) {

this.delegate = delegate;
this.validator = validator;
}

@Override
public boolean isReadable(
final Class type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType) {

return this.delegate.isReadable(type, genericType, annotations, mediaType);
}

@Override
public Object readFrom(
final Class type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType,
final MultivaluedMap httpHeaders,
final InputStream entityStream)
throws IOException,
WebApplicationException {
....
}

}
```

### Problem : I loose all configuration of my ObjectMapper because, with the last config, Jersey (or Jackson feature) create a new ObjectMapper.
#### Affected Versions
[2.19]

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.