org.springframework.boot.autoconfigure.jackson.JacksonProperties Java Examples

The following examples show how to use org.springframework.boot.autoconfigure.jackson.JacksonProperties. 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: LocalDateTimeSerializerConfig.java    From gateway with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取序列化组件
 *
 * @param jacksonProperties JacksonProperties
 * @return SimpleModule
 */
@Bean
public SimpleModule localDateTimeSerializationModule(JacksonProperties jacksonProperties) {
    SimpleModule module = new SimpleModule();
    module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(LocalDateTime.class, jacksonProperties.getDateFormat()));

    return module;
}
 
Example #2
Source File: ApiConfiguration.java    From open-cloud with MIT License 5 votes vote down vote up
/**
 * Jackson全局配置
 *
 * @param properties
 * @return
 */
@Bean
@Primary
public JacksonProperties jacksonProperties(JacksonProperties properties) {
    properties.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
    properties.getSerialization().put(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
    properties.setDateFormat("yyyy-MM-dd HH:mm:ss");
    properties.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    log.info("JacksonProperties [{}]", properties);
    return properties;
}
 
Example #3
Source File: VaadinConnectController.java    From flow with Apache License 2.0 5 votes vote down vote up
private ObjectMapper createVaadinConnectObjectMapper(ApplicationContext context) {
  Jackson2ObjectMapperBuilder builder = context.getBean(Jackson2ObjectMapperBuilder.class);
  ObjectMapper objectMapper = builder.createXmlMapper(false).build();
  JacksonProperties jacksonProperties = context
          .getBean(JacksonProperties.class);
  if (jacksonProperties.getVisibility().isEmpty()) {
      objectMapper.setVisibility(PropertyAccessor.ALL,
              JsonAutoDetect.Visibility.ANY);
  }
  return objectMapper;
}
 
Example #4
Source File: VaadinConnectControllerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_Never_UseSpringObjectMapper() {
    ApplicationContext contextMock = mock(ApplicationContext.class);
    ObjectMapper mockSpringObjectMapper = mock(ObjectMapper.class);
    ObjectMapper mockOwnObjectMapper = mock(ObjectMapper.class);
    Jackson2ObjectMapperBuilder mockObjectMapperBuilder = mock(Jackson2ObjectMapperBuilder.class);
    JacksonProperties mockJacksonProperties = mock(JacksonProperties.class);
    when(contextMock.getBean(ObjectMapper.class))
            .thenReturn(mockSpringObjectMapper);
    when(contextMock.getBean(JacksonProperties.class))
            .thenReturn(mockJacksonProperties);
    when(contextMock.getBean(Jackson2ObjectMapperBuilder.class))
            .thenReturn(mockObjectMapperBuilder);
    when(mockObjectMapperBuilder.createXmlMapper(false))
            .thenReturn(mockObjectMapperBuilder);
    when(mockObjectMapperBuilder.build())
            .thenReturn(mockOwnObjectMapper);
    when(mockJacksonProperties.getVisibility())
            .thenReturn(Collections.emptyMap());
    new VaadinConnectController(null,
            mock(VaadinConnectAccessChecker.class),
            mock(EndpointNameChecker.class),
            mock(ExplicitNullableTypeChecker.class),
            contextMock,
            mock(ServletContext.class));

    verify(contextMock, never()).getBean(ObjectMapper.class);
    verify(contextMock, times(1)).getBean(Jackson2ObjectMapperBuilder.class);
    verify(contextMock, times(1)).getBean(JacksonProperties.class);
    verify(mockOwnObjectMapper, times(1)).setVisibility(
            PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
}
 
Example #5
Source File: VaadinConnectControllerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_NotOverrideVisibility_When_JacksonPropertiesProvideVisibility() {
    ApplicationContext contextMock = mock(ApplicationContext.class);
    ObjectMapper mockDefaultObjectMapper = mock(ObjectMapper.class);
    ObjectMapper mockOwnObjectMapper = mock(ObjectMapper.class);
    Jackson2ObjectMapperBuilder mockObjectMapperBuilder = mock(Jackson2ObjectMapperBuilder.class);
    JacksonProperties mockJacksonProperties = mock(JacksonProperties.class);
    when(contextMock.getBean(ObjectMapper.class))
            .thenReturn(mockDefaultObjectMapper);
    when(contextMock.getBean(JacksonProperties.class))
            .thenReturn(mockJacksonProperties);
    when(contextMock.getBean(Jackson2ObjectMapperBuilder.class))
            .thenReturn(mockObjectMapperBuilder);
    when(mockObjectMapperBuilder.createXmlMapper(false))
            .thenReturn(mockObjectMapperBuilder);
    when(mockObjectMapperBuilder.build())
            .thenReturn(mockOwnObjectMapper);
    when(mockJacksonProperties.getVisibility())
            .thenReturn(Collections.singletonMap(PropertyAccessor.ALL,
                    JsonAutoDetect.Visibility.PUBLIC_ONLY));
    new VaadinConnectController(null,
            mock(VaadinConnectAccessChecker.class),
            mock(EndpointNameChecker.class),
            mock(ExplicitNullableTypeChecker.class),
            contextMock,
            mock(ServletContext.class));

    verify(contextMock, never()).getBean(ObjectMapper.class);
    verify(contextMock, times(1)).getBean(Jackson2ObjectMapperBuilder.class);
    verify(mockDefaultObjectMapper, never()).setVisibility(
            PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    verify(mockOwnObjectMapper, never()).setVisibility(
            PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    verify(contextMock, times(1)).getBean(JacksonProperties.class);
}