public static void main(String[] args) throws JsonMappingException { ObjectMapper jsonMapper = new ObjectMapper(); AnnotationIntrospector introspector = new AnnotationIntrospector.Pair(new JaxbAnnotationIntrospector(), new JacksonAnnotationIntrospector()); jsonMapper.setAnnotationIntrospector(introspector); JsonSchema schema = jsonMapper.generateJsonSchema(Testi.class); if(Testi.class.getAnnotation(XmlRootElement.class)!=null && !Testi.class.getAnnotation(XmlRootElement.class).name().equals("##default")) schema.getSchemaNode().put("name", Testi.class.getAnnotation(XmlRootElement.class).name()); else if(Testi.class.getAnnotation(XmlType.class)!=null && !Testi.class.getAnnotation(XmlType.class).name().equals("##default")) schema.getSchemaNode().put("name", Testi.class.getAnnotation(XmlType.class).name()); else schema.getSchemaNode().put("name", Testi.class.getSimpleName()); String schemaJson = schema.toString(); System.out.println(schemaJson); }
@Bean(name="defaultObjectMapper") public ObjectMapper getObjectMapper() { if(defaultObjectMapper==null) { ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector primary = new JaxbAnnotationIntrospector(); AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); mapper.getDeserializationConfig().setAnnotationIntrospector(pair); mapper.getDeserializationConfig().set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.getSerializationConfig().setAnnotationIntrospector(pair); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); this.defaultObjectMapper = mapper; } return defaultObjectMapper; }
/** * Update the generator with a default codec and pretty printer * * @param jsonFactory Factory to set as codec * @param jsonGenerator Generator to configure */ private static void updateGenerator(JsonFactory jsonFactory, JsonGenerator jsonGenerator) { ObjectMapper mapper = new ObjectMapper(jsonFactory); //Update the annotation interceptor to also include jaxb annotations as a second choice AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); AnnotationIntrospector secondary = new JaxbAnnotationIntrospector() { /** * BUG FIX: * By contract, if findSerializationInclusion didn't encounter any annotations that should change the * inclusion value, it should return the default value it has received; but actually returns null, which * overrides the NON_NULL option we set. * The doc states issue JACKSON-256 which was supposed to be fixed in 1.5.0. *twilight zone theme song* */ @Override public JsonSerialize.Inclusion findSerializationInclusion(Annotated a, JsonSerialize.Inclusion defValue) { JsonSerialize.Inclusion inclusion = super.findSerializationInclusion(a, defValue); if (inclusion == null) { return defValue; } return inclusion; } }; AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); mapper.getSerializationConfig().setAnnotationIntrospector(pair); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); jsonGenerator.setCodec(mapper); jsonGenerator.useDefaultPrettyPrinter(); }
/** * Update the parser with a default codec * * @param jsonFactory Factory to set as codec * @param jsonParser Parser to configure */ private static void updateParser(JsonFactory jsonFactory, JsonParser jsonParser) { AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(); AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); ObjectMapper mapper = new ObjectMapper(jsonFactory); mapper.getSerializationConfig().setAnnotationIntrospector(pair); mapper.getDeserializationConfig().setAnnotationIntrospector(pair); mapper.getDeserializationConfig().disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES); jsonParser.setCodec(mapper); }
public JsonProvider() { ObjectMapper mapper = getMapper(); //Update the annotation interceptor to also include jaxb annotations as a second choice AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(); AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); mapper.getSerializationConfig().setAnnotationIntrospector(pair); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); mapper.getDeserializationConfig().setAnnotationIntrospector(pair); //Ignore missing properties mapper.getDeserializationConfig().disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES); }
public JacksonJaxbContextResolver() { this.objectMapper = new ObjectMapper(); final AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); final AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(); final AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); this.objectMapper.getDeserializationConfig().setAnnotationIntrospector(pair); this.objectMapper.getSerializationConfig().setAnnotationIntrospector(pair); }
public ObjectMapperFactoryBean() { this.objectMapper = new ObjectMapper(); this.objectMapper.setSerializationInclusion(Inclusion.NON_NULL); this.objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(); AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); this.objectMapper.setAnnotationIntrospector(pair); }
@Override public ObjectMapper locateMapper(Class<?> type, MediaType mediaType) { ObjectMapper mapper = super.locateMapper(type, mediaType); AnnotationIntrospector introspector = new AnnotationIntrospector.Pair( new JaxbAnnotationIntrospector(), new JacksonAnnotationIntrospector() ); mapper.setAnnotationIntrospector(introspector); //mapper.setSerializationInclusion(Inclusion.NON_NULL); return mapper; }
/** * Creates json mapper. * * @return json mapper */ public static ObjectMapper createJsonMapper() { final AnnotationIntrospector jaxb = new JaxbAnnotationIntrospector(); final AnnotationIntrospector jackson = new JacksonAnnotationIntrospector(); final AnnotationIntrospector pair = new AnnotationIntrospector.Pair(jackson, jaxb); final ObjectMapper mapper = new ObjectMapper(); mapper.getDeserializationConfig().withAnnotationIntrospector(pair); mapper.getSerializationConfig().withAnnotationIntrospector(pair); return mapper; }
public static ObjectMapper createJsonMapper() { final AnnotationIntrospector jaxb = new JaxbAnnotationIntrospector(); final AnnotationIntrospector jackson = new JacksonAnnotationIntrospector(); final AnnotationIntrospector pair = new AnnotationIntrospector.Pair(jackson, jaxb); final ObjectMapper mapper = new ObjectMapper(); mapper.getDeserializationConfig().withAnnotationIntrospector(pair); mapper.getSerializationConfig().withAnnotationIntrospector(pair); return mapper; }
public JacksonProvider() { AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(); AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); mapper.setAnnotationIntrospector(new AnnotationIntrospector.Pair(introspector, secondary)); mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); }
private static Pair createJaxbJacksonAnnotationIntrospector() { AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(); AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector(); return new AnnotationIntrospector.Pair(jacksonIntrospector, jaxbIntrospector); }