Uploaded image for project: 'Application Server 7'
  1. Application Server 7
  2. AS7-4334

RESTeasy: Cannot configure Jackson ObjectMapper via ContextResolver nor Application.getSingletons()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: Major Major
    • None
    • 7.1.1.Final
    • REST
    • None

      This doesn't work:

      @Provider @Produces(MediaType.APPLICATION_JSON)
      public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
      
      	private transient Logger log = LoggerFactory.getLogger(JacksonContextResolver.class);
      	private ObjectMapper objectMapper;
      
      	public JacksonContextResolver() throws JsonGenerationException, JsonMappingException, IOException {
      		log.info("Using my own Jackson ObjectMapper");
      		objectMapper = new ObjectMapper();
      		objectMapper.configure(Feature.INDENT_OUTPUT, true);
      		objectMapper.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
      		objectMapper.configure(Feature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
      		
      		log.info("Test date: {}", objectMapper.writeValueAsString(new DateTime()));
      		log.info("Test pretty output: {}", objectMapper
      				.writeValueAsString(new Article("Launching AksiMata",
      						"AksiMata : Ada apa di sana?",
      						"Webapp yang keren dan oke punya.", "Hendy Irawan")));
      	}
      	
      	@Override
      	public ObjectMapper getContext(Class<?> type) {
      		return objectMapper;
      	}
      }
      

      The log correctly tests the custom ObjectMapper works fine, however when returning the output, seems like it's still using the "default" ObjectMapper.

      This also doesn't work:

      @Override
      public Set<Object> getSingletons() {
      	Set<Object> singletons = new HashSet<Object>(super.getSingletons());
      	ObjectMapper objectMapper = new ObjectMapper();
      	objectMapper.configure(Feature.INDENT_OUTPUT, true);
      	objectMapper.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
      	objectMapper.configure(Feature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
      	singletons.add(objectMapper);
      	return singletons;
      }
      

      Sample project to reproduce the issue is at:
      https://github.com/soluvas/ear-sample/tarball/resteasy-jackson-bug

      After deploy, check the output via:
      http://localhost:8080/api/article/

      Bug is almost similar to #AS7-1408.

              sdouglas1@redhat.com Stuart Douglas (Inactive)
              HendyIrawan Hendy Irawan (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: