@Override public void afterPropertiesSet() throws Exception { jsonProvider.setMapper(jacksonMapper); jacksonMapper.setSerializerFactory(serializerFactory); jacksonMapper.setSerializationInclusion(Inclusion.NON_NULL); serializerFactory.addSpecificMapping(BigInteger.class, this); }
public static void main(String[] args) throws Exception { Bean bean = new Bean("mr bean", "goofy", "secret!"); ObjectMapper mapper = new ObjectMapper(); mapper.setSerializerFactory(new CustomBeanFactory()); // First, without enabling view handling: System.out.println("With default serializer: " + mapper.writeValueAsString(bean)); // Then with customized view-handling /* note: View class being passed is irrelevant since we customize handling) * Also: setting view would not be necessary if we just completely * overrode handling (we didn't, mostly to show more flexible * approach: conceivably you could have another way for passing * your own view id system, using ThreadLocal or some other * configurable part of <code>SerializationConfig</code> or * <code>SerializerProvider</code>) */ /* note: if we wanted use 'writeValueAsString', would have to call * 'mapper.getSerializationConfig().setSerializationView(...)' first */ String json = mapper.viewWriter(String.class).writeValueAsString(bean); System.out.println("With custom serializer: " + json); }