com.fasterxml.jackson.module.paramnames.ParameterNamesModule Java Examples

The following examples show how to use com.fasterxml.jackson.module.paramnames.ParameterNamesModule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomConfig.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
private void setConfigForJdk8(ObjectMapper objectMapper) {
    JavaTimeModule timeModule = new JavaTimeModule();
    timeModule.addSerializer(LocalDate.class,
        new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    timeModule.addDeserializer(LocalDate.class,
        new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

    timeModule.addSerializer(Date.class, new DateSerializer());
    timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
        .registerModule(timeModule).registerModule(new ParameterNamesModule())
        .registerModule(new Jdk8Module());
}
 
Example #2
Source File: ValidatedConfiguration.java    From divolte-collector with Apache License 2.0 6 votes vote down vote up
private static DivolteConfiguration mapped(final Config input) throws IOException {
    final Config resolved = input.resolve();
    final ObjectMapper mapper = new ObjectMapper();

    // snake_casing
    mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

    // Ignore unknown stuff in the config
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);

    // Deserialization for Duration
    final SimpleModule module = new SimpleModule("Configuration Deserializers");
    module.addDeserializer(Duration.class, new DurationDeserializer());
    module.addDeserializer(Properties.class, new PropertiesDeserializer());

    mapper.registerModules(
            new Jdk8Module(),                   // JDK8 types (Optional, etc.)
            new GuavaModule(),                  // Guava types (immutable collections)
            new ParameterNamesModule(),         // Support JDK8 parameter name discovery
            module                              // Register custom deserializers module
            );

    return mapper.readValue(new HoconTreeTraversingParser(resolved.root()), DivolteConfiguration.class);
}
 
Example #3
Source File: JsonDBConfig.java    From jsondb-core with MIT License 6 votes vote down vote up
public JsonDBConfig(String dbFilesLocationString, String baseScanPackage,
    ICipher cipher, boolean compatibilityMode, Comparator<String> schemaComparator) {

  this.charset = Charset.forName("UTF-8");
  this.dbFilesLocationString = dbFilesLocationString;
  this.dbFilesLocation = new File(dbFilesLocationString);
  this.dbFilesPath = dbFilesLocation.toPath();
  this.baseScanPackage = baseScanPackage;
  this.cipher = cipher;

  this.compatibilityMode = compatibilityMode;
  this.objectMapper = new ObjectMapper()
          .registerModule(new ParameterNamesModule())
          .registerModule(new Jdk8Module())
          .registerModule(new JavaTimeModule());

  if (compatibilityMode) {
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  }

  if (null == schemaComparator) {
    this.schemaComparator = new DefaultSchemaVersionComparator();
  } else {
    this.schemaComparator = schemaComparator;
  }
}
 
Example #4
Source File: ObjectMapperProvider.java    From jersey-jwt with MIT License 6 votes vote down vote up
private static ObjectMapper createObjectMapper() {

        ObjectMapper mapper = new ObjectMapper();

        mapper.registerModule(new Jdk8Module());
        mapper.registerModule(new JavaTimeModule());
        mapper.registerModule(new ParameterNamesModule());

        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        return mapper;
    }
 
Example #5
Source File: ObjectMapperContextResolver.java    From microservices-springboot with MIT License 6 votes vote down vote up
private ObjectMapper createObjectMapper() {

        ObjectMapper mapper = new ObjectMapper();

        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        mapper.registerModule(new Jdk8Module());
        mapper.registerModule(new JavaTimeModule());
        mapper.registerModule(new ParameterNamesModule());

        return mapper;
    }
 
Example #6
Source File: RestHelper.java    From taskana with Apache License 2.0 6 votes vote down vote up
/**
 * Return a REST template which is capable of dealing with responses in HAL format.
 *
 * @return RestTemplate
 */
private static RestTemplate getRestTemplate() {
  ObjectMapper mapper = new ObjectMapper();
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  mapper.registerModule(new Jackson2HalModule());
  mapper
      .registerModule(new ParameterNamesModule())
      .registerModule(new Jdk8Module())
      .registerModule(new JavaTimeModule());

  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setSupportedMediaTypes(Collections.singletonList(MediaTypes.HAL_JSON));
  converter.setObjectMapper(mapper);

  RestTemplate template = new RestTemplate();
  // important to add first to ensure priority
  template.getMessageConverters().add(0, converter);
  return template;
}
 
Example #7
Source File: RestHelper.java    From taskana with Apache License 2.0 6 votes vote down vote up
/**
 * Return a REST template which is capable of dealing with responses in HAL format.
 *
 * @return RestTemplate
 */
private static RestTemplate getRestTemplate() {
  ObjectMapper mapper = new ObjectMapper();
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  mapper.registerModule(new Jackson2HalModule());
  mapper
      .registerModule(new ParameterNamesModule())
      .registerModule(new Jdk8Module())
      .registerModule(new JavaTimeModule());
  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setSupportedMediaTypes(Collections.singletonList(MediaTypes.HAL_JSON));
  converter.setObjectMapper(mapper);

  RestTemplate template = new RestTemplate();
  // important to add first to ensure priority
  template.getMessageConverters().add(0, converter);
  return template;
}
 
Example #8
Source File: ObjectMapperProvider.java    From jersey-jwt-springsecurity with MIT License 6 votes vote down vote up
private static ObjectMapper createObjectMapper() {

        ObjectMapper mapper = new ObjectMapper();

        mapper.registerModule(new Jdk8Module());
        mapper.registerModule(new JavaTimeModule());
        mapper.registerModule(new ParameterNamesModule());

        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        return mapper;
    }
 
Example #9
Source File: JacksonModule.java    From proteus with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure()
{
    ObjectMapper objectMapper = new ObjectMapper();

    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
    objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
    objectMapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true);
    objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);

    objectMapper.registerModule(new AfterburnerModule());
    objectMapper.registerModule(new Jdk8Module());
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.registerModule(new ParameterNamesModule());

    this.bind(ObjectMapper.class).toInstance(objectMapper);
}
 
Example #10
Source File: PollingState.java    From botbuilder-java with MIT License 6 votes vote down vote up
/**
 * Initializes an object mapper.
 *
 * @param mapper the mapper to initialize
 * @return the initialized mapper
 */
private static ObjectMapper initMapper(ObjectMapper mapper) {
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true)
            .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            .registerModule(new ParameterNamesModule())
            .registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule())
            .registerModule(ByteArraySerializer.getModule())
            .registerModule(Base64UrlSerializer.getModule())
            .registerModule(HeadersSerializer.getModule());
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
    return mapper;
}
 
Example #11
Source File: JacksonAdapter.java    From botbuilder-java with MIT License 6 votes vote down vote up
/**
 * Initializes an instance of JacksonMapperAdapter with default configurations
 * applied to the object mapper.
 *
 * @param mapper the object mapper to use.
 */
private static ObjectMapper initializeObjectMapper(ObjectMapper mapper) {
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true)
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            .registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule())
            .registerModule(new ParameterNamesModule())
            .registerModule(ByteArraySerializer.getModule())
            .registerModule(Base64UrlSerializer.getModule())
            .registerModule(HeadersSerializer.getModule());
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
    return mapper;
}
 
Example #12
Source File: JacksonConfiguration.java    From cola-cloud with MIT License 6 votes vote down vote up
@Bean
public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();

    //设置日期格式
    //TODO 需要兼容时间和日期格式
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    //Long 转String类型,否则js丢失精度
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
    //支持jdk8新特性
    objectMapper
            .registerModule(new ParameterNamesModule())
            .registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule())
            .registerModule(simpleModule)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
    return mappingJackson2HttpMessageConverter;
}
 
Example #13
Source File: CustomConfig.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
private void setConfigForJdk8(ObjectMapper objectMapper) {
    JavaTimeModule timeModule = new JavaTimeModule();
    timeModule.addSerializer(LocalDate.class,
        new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    timeModule.addDeserializer(LocalDate.class,
        new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

    timeModule.addSerializer(Date.class, new DateSerializer());
    timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
        .registerModule(timeModule).registerModule(new ParameterNamesModule())
        .registerModule(new Jdk8Module());
}
 
Example #14
Source File: IntegrationTestSettingsLoader.java    From line-bot-sdk-java with Apache License 2.0 5 votes vote down vote up
public static IntegrationTestSettings load() throws IOException {
    // Do not run all test cases in this class when src/test/resources/integration_test_settings.yml doesn't
    // exist.
    Assume.assumeTrue("exists integration_test_settings.yml in resource directory",
                      TEST_RESOURCE != null);

    return new ObjectMapper(new YAMLFactory())
            .registerModule(new ParameterNamesModule())
            .readValue(TEST_RESOURCE, IntegrationTestSettings.class);
}
 
Example #15
Source File: CamundaJacksonFormatConfiguratorParameterNames.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(JacksonJsonDataFormat dataFormat) {
  ObjectMapper mapper = dataFormat.getObjectMapper();
  final ParameterNamesModule parameterNamesModule = new ParameterNamesModule();

  mapper.registerModule(parameterNamesModule);
}
 
Example #16
Source File: JsonViewSerializerTest.java    From json-view with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setup() {
  this.serializer = new JsonViewSerializer();
  sut = new ObjectMapper()
      .registerModule(new JsonViewModule(serializer))
      .registerModule(new ParameterNamesModule())
      .registerModule(new Jdk8Module())
      .registerModule(new JavaTimeModule());
}
 
Example #17
Source File: IssueTrackerConfiguration.java    From spring-data-dev-tools with Apache License 2.0 5 votes vote down vote up
@Bean
ObjectMapper jacksonObjectMapper() {

	ObjectMapper mapper = new ObjectMapper();

	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	mapper.setSerializationInclusion(Include.NON_NULL);
	mapper.registerModule(new ParameterNamesModule(Mode.PROPERTIES));
	mapper.registerModule(new SyntheticLambdaFactoryMethodIgnoringModule());

	return mapper;
}
 
Example #18
Source File: CamundaJacksonFormatConfiguratorParameterNames.java    From camunda-bpm-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(JacksonJsonDataFormat dataFormat) {
  ObjectMapper mapper = dataFormat.getObjectMapper();
  final ParameterNamesModule parameterNamesModule = new ParameterNamesModule();

  mapper.registerModule(parameterNamesModule);
}
 
Example #19
Source File: ShopRestController.java    From state-machine with Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public ObjectMapper objectMapper() {
    return new ObjectMapper() //
            .setVisibility(PropertyAccessor.FIELD, Visibility.PUBLIC_ONLY)
            .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) //
            .registerModule(new Jdk8Module()) //
            .registerModule(new ParameterNamesModule());
}
 
Example #20
Source File: MvcTest.java    From friendly-id with Apache License 2.0 5 votes vote down vote up
protected Jackson2ObjectMapperBuilder jacksonBuilder() {
	Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
	builder.modules(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES), new JavaTimeModule(), new FriendlyIdModule());
	builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
	builder.simpleDateFormat("yyyy-MM-dd");
	builder.indentOutput(true);
	return builder;
}
 
Example #21
Source File: FieldWithoutFriendlyIdTest.java    From friendly-id with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDeserializeUuuidsValueObject() throws Exception {
	mapper = mapper(new ParameterNamesModule());

	String json = "{\"rawUuid\":\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\",\"friendlyId\":\"7Jsg6CPDscHawyJfE70b9x\"}";

	Bar deserialized = mapper.readValue(json, Bar.class);

	assertThat(deserialized.getRawUuid()).isEqualTo(uuid);
	assertThat(deserialized.getFriendlyId()).isEqualTo(uuid);
}
 
Example #22
Source File: FieldWithoutFriendlyIdTest.java    From friendly-id with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeUuidsInValueObject() throws Exception {
	mapper = mapper(new ParameterNamesModule());

	Bar bar = new Bar(uuid, uuid);

	String json = mapper.writeValueAsString(bar);

	assertThat(json).isEqualToIgnoringWhitespace(
			"{\"rawUuid\":\"f088ce5b-9279-4cc3-946a-c15ad740dd6d\",\"friendlyId\":\"7Jsg6CPDscHawyJfE70b9x\"}"
	);
}
 
Example #23
Source File: PcObjectMapper.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
public static void buidMvcMessageConverter(List<HttpMessageConverter<?>> converters) {
	MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
	SimpleModule simpleModule = new SimpleModule();
	simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
	simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
	ObjectMapper objectMapper = new ObjectMapper()
			.registerModule(new ParameterNamesModule())
			.registerModule(new Jdk8Module())
			.registerModule(new JavaTimeModule())
			.registerModule(simpleModule);
	objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	jackson2HttpMessageConverter.setObjectMapper(objectMapper);
	converters.add(jackson2HttpMessageConverter);
}
 
Example #24
Source File: JsonHelper.java    From spydra with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper objectMapper() {
  return new ObjectMapper()
      .setPropertyNamingStrategy(SNAKE_CASE)
      .enable(ACCEPT_SINGLE_VALUE_AS_ARRAY)
      .registerModule(new JavaTimeModule())
      .registerModule(new Jdk8Module())
      .registerModule(new ParameterNamesModule())
      .setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
 
Example #25
Source File: Server.java    From xrpc with Apache License 2.0 5 votes vote down vote up
private Server(XConfig config, int port) {
  this.config = config;
  this.port = port >= 0 ? port : config.port();
  this.sslContext = config.sslContext();
  this.healthCheckRegistry = new HealthCheckRegistry(config.asyncHealthCheckThreadCount());

  // This adds support for normal constructor binding.
  // See: https://github.com/FasterXML/jackson-modules-java8/tree/master/parameter-names
  ObjectMapper mapper =
      new ObjectMapper().registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));

  // Json encoder for Proto
  JsonFormat.Printer printer = JsonFormat.printer().omittingInsignificantWhitespace();

  // Default instances for protobuf generated classes.
  ProtoDefaultInstances protoDefaultInstances = new ProtoDefaultInstances();

  this.contextBuilder =
      ServerContext.builder()
          .requestMeter(metricRegistry.meter("requests"))
          .encoders(
              Encoders.builder()
                  .defaultContentType(config.defaultContentType())
                  .encoder(new JsonEncoder(mapper, printer))
                  // TODO (AD): For now we won't support text/plain encoding.
                  // Leaving this here as a placeholder.
                  // .encoder(new TextEncoder())
                  .encoder(new ProtoEncoder())
                  .build())
          .decoders(
              Decoders.builder()
                  .defaultContentType(config.defaultContentType())
                  .decoder(new JsonDecoder(mapper, protoDefaultInstances))
                  .decoder(new ProtoDecoder(protoDefaultInstances))
                  .build())
          .exceptionHandler(ResponseFactory::exception);

  addResponseCodeMeters(contextBuilder, metricRegistry);
}
 
Example #26
Source File: XmlModule.java    From proteus with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {

    XMLInputFactory inputFactory = new WstxInputFactory();
    inputFactory.setProperty(WstxInputProperties.P_MAX_ATTRIBUTE_SIZE, 32000);

    bind(XMLInputFactory.class).toInstance(inputFactory);

    XMLOutputFactory outputFactory = new WstxOutputFactory();
    outputFactory.setProperty(WstxOutputProperties.P_OUTPUT_CDATA_AS_TEXT, true);

    bind(XMLOutputFactory.class).toInstance(outputFactory);

    XmlFactory xmlFactory = new XmlFactory(inputFactory, outputFactory);

    XmlMapper xmlMapper = new XmlMapper(xmlFactory);
    xmlMapper.registerModule(new JavaTimeModule())
             .registerModule(new ParameterNamesModule())
             .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
             .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    xmlMapper.enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION);

    bind(XmlMapper.class).toInstance(xmlMapper);

}
 
Example #27
Source File: Main.java    From fahrschein with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {

        final ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);

        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.registerModule(new Jdk8Module());
        objectMapper.registerModule(new MoneyModule());
        objectMapper.registerModule(new ParameterNamesModule());

        final Listener<SalesOrderPlaced> listener = events -> {
            if (Math.random() < 0.0000001) {
                // For testing reconnection logic
                throw new EventProcessingException("Random failure");
            } else {
                for (SalesOrderPlaced salesOrderPlaced : events) {
                    final SalesOrder order = salesOrderPlaced.getSalesOrder();
                    LOG.info("Received sales order [{}] created at [{}]", order.getOrderNumber(), order.getCreatedAt());
                }
            }
        };

        //subscriptionListen(objectMapper, listener);

        subscriptionListenHttpComponents(objectMapper, listener);

        //subscriptionListenSpringAdapter(objectMapper, listener);

        //subscriptionListenWithPositionCursors(objectMapper, listener);

        //subscriptionMultipleEvents(objectMapper);

        //simpleListen(objectMapper, listener);

        //persistentListen(objectMapper, listener);

        //subscriptionCreateWithAuthorization(objectMapper, listener);

        //multiInstanceListen(objectMapper, listener);
    }
 
Example #28
Source File: JSONConfiguration.java    From spring-boot-jpa-data-rest-soft-delete with MIT License 4 votes vote down vote up
@Bean
public Module ParameterNamesModule() {
	return new ParameterNamesModule();
}
 
Example #29
Source File: WebConfig.java    From hdw-dubbo with Apache License 2.0 4 votes vote down vote up
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

    ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

    SimpleModule simpleModule = new SimpleModule();
    // Long类型序列化成字符串,避免Long精度丢失
    simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);

    // XSS序列化
    simpleModule.addSerializer(String.class, new XssJacksonSerializer());
    simpleModule.addDeserializer(String.class, new XssJacksonDeserializer());

    // Date序列化
    simpleModule.addSerializer(Date.class, new JacksonDateSerializer());
    simpleModule.addDeserializer(Date.class, new JacksonDateDeserializer());

    // Integer、Double反序列化
    simpleModule.addDeserializer(Integer.class, new JacksonIntegerDeserializer());
    simpleModule.addDeserializer(Double.class, new JacksonDoubleDeserializer());

    // jdk8日期序列化和反序列化设置
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
    javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));

    javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
    javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));

    javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
    javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));

    objectMapper.registerModule(simpleModule)
            .registerModule(javaTimeModule).registerModule(new ParameterNamesModule());

    jackson2HttpMessageConverter.setObjectMapper(objectMapper);

    //放到第一个
    converters.add(0, jackson2HttpMessageConverter);

}
 
Example #30
Source File: JacksonConfig.java    From spring-boot-plus with Apache License 2.0 4 votes vote down vote up
@Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();

        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

        SimpleModule simpleModule = new SimpleModule();
        // Long类型序列化成字符串,避免Long精度丢失
//        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);

        // XSS序列化
        if (enableXss){
            simpleModule.addSerializer(String.class, new XssJacksonSerializer());
            simpleModule.addDeserializer(String.class, new XssJacksonDeserializer());
        }

        // Date
        simpleModule.addSerializer(Date.class, new JacksonDateSerializer());
        simpleModule.addDeserializer(Date.class, new JacksonDateDeserializer());

        simpleModule.addDeserializer(Integer.class, new JacksonIntegerDeserializer());
        simpleModule.addDeserializer(Double.class, new JacksonDoubleDeserializer());

        // jdk8日期序列化和反序列化设置
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.YYYY_MM_DD_HH_MM_SS)));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.YYYY_MM_DD_HH_MM_SS)));

        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.YYYY_MM_DD)));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.YYYY_MM_DD)));

        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.HH_MM_SS)));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.HH_MM_SS)));

        objectMapper.registerModule(simpleModule).registerModule(javaTimeModule).registerModule(new ParameterNamesModule());

        jackson2HttpMessageConverter.setObjectMapper(objectMapper);

        //放到第一个
        converters.add(0, jackson2HttpMessageConverter);
    }