Java Code Examples for org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#setObjectMapper()

The following examples show how to use org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#setObjectMapper() . 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: 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 2
Source File: RestTemplateFakeReactiveHttpClient.java    From feign-reactive with Apache License 2.0 6 votes vote down vote up
RestTemplateFakeReactiveHttpClient(MethodMetadata methodMetadata,
    RestTemplate restTemplate,
    boolean acceptGzip) {
  this.restTemplate = restTemplate;
  this.acceptGzip = acceptGzip;

  ObjectMapper mapper = new ObjectMapper();
  mapper.registerModule(new JavaTimeModule());

  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setObjectMapper(mapper);
  restTemplate.getMessageConverters().add(0, converter);


  final Type returnType = methodMetadata.returnType();
  returnPublisherType = ((ParameterizedType) returnType).getRawType();
  returnActualType = forType(
      resolveLastTypeParameter(returnType, (Class<?>) returnPublisherType));
}
 
Example 3
Source File: TestChronosController.java    From chronos with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings( "rawtypes" )
@Before
public void setUp() throws Exception{
  when(jobDao.getJobRuns(null, AgentConsumer.LIMIT_JOB_RUNS)).thenReturn(
    new ConcurrentSkipListMap<Long, CallableJob>());
  agentDriver  = new AgentDriver(jobDao, reporting);
  agentConsumer  =
    spy(new AgentConsumer(jobDao, reporting, "testing.hostname.com",
      new MailInfo("", "", "", ""),
      Session.getDefaultInstance(new Properties()),
      drivers, 10, numOfConcurrentReruns, maxReruns, 60, 1));
  controller = new ChronosController(jobDao, agentDriver, agentConsumer, drivers, folder.getRoot().getPath());

  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setObjectMapper(new ChronosMapper());
  HttpMessageConverter[] messageConverters =
          new HttpMessageConverter[] {converter};

  this.mockMvc = MockMvcBuilders.standaloneSetup(controller)
    .setMessageConverters(messageConverters).build();
}
 
Example 4
Source File: RestTemplateFactory.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
public static RestOperations createCommonsHttpRestTemplate(int maxConnPerRoute, int maxConnTotal,
                                                           int connectTimeout, int soTimeout, int retryTimes, RetryPolicyFactory retryPolicyFactory) {
    HttpClient httpClient = HttpClientBuilder.create()
            .setMaxConnPerRoute(maxConnPerRoute)
            .setMaxConnTotal(maxConnTotal)
            .setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(soTimeout).build())
            .setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(connectTimeout).build())
            .build();
    ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
    RestTemplate restTemplate = new RestTemplate(factory);
    //set jackson mapper
    for (HttpMessageConverter<?> hmc : restTemplate.getMessageConverters()) {
        if (hmc instanceof MappingJackson2HttpMessageConverter) {
            ObjectMapper objectMapper = createObjectMapper();
            MappingJackson2HttpMessageConverter mj2hmc = (MappingJackson2HttpMessageConverter) hmc;
            mj2hmc.setObjectMapper(objectMapper);
        }
    }

    return (RestOperations) Proxy.newProxyInstance(RestOperations.class.getClassLoader(),
            new Class[]{RestOperations.class},
            new RetryableRestOperationsHandler(restTemplate, retryTimes, retryPolicyFactory));
}
 
Example 5
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 6
Source File: TenantInteractionScenarioTest.java    From metamodel-membrane with Apache License 2.0 6 votes vote down vote up
@Before
public void init() {
    final MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
    messageConverter.setObjectMapper(new JacksonConfig().objectMapper());

    final TenantRegistry tenantRegistry = new InMemoryTenantRegistry();
    final TenantController tenantController = new TenantController(tenantRegistry);
    final DataSourceController dataSourceController = new DataSourceController(tenantRegistry);
    final SchemaController schemaController = new SchemaController(tenantRegistry);
    final TableController tableController = new TableController(tenantRegistry);
    final ColumnController columnController = new ColumnController(tenantRegistry);
    final QueryController queryController = new QueryController(tenantRegistry);
    final TableDataController tableDataController = new TableDataController(tenantRegistry);

    mockMvc = MockMvcBuilders.standaloneSetup(tenantController, dataSourceController, schemaController,
            tableController, columnController, queryController, tableDataController).setMessageConverters(
                    messageConverter).build();
}
 
Example 7
Source File: ApiConfiguration.java    From open-cloud with MIT License 6 votes vote down vote up
/**
 * 转换器全局配置
 *
 * @param converters
 * @return
 */
@Bean
public HttpMessageConverters httpMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    // 忽略为空的字段
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.getSerializationConfig().withFeatures(
            SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    /**
     * 序列换成json时,将所有的long变成string
     * js中long过长精度丢失
     */
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
    objectMapper.registerModule(simpleModule);
    jackson2HttpMessageConverter.setObjectMapper(objectMapper);
    log.info("MappingJackson2HttpMessageConverter [{}]", jackson2HttpMessageConverter);
    return new HttpMessageConverters(jackson2HttpMessageConverter);
}
 
Example 8
Source File: RestTemplateTransportClientFactory.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
/**
 * Provides the serialization configurations required by the Eureka Server. JSON
 * content exchanged with eureka requires a root node matching the entity being
 * serialized or deserialized. Achived with
 * {@link SerializationFeature#WRAP_ROOT_VALUE} and
 * {@link DeserializationFeature#UNWRAP_ROOT_VALUE}.
 * {@link PropertyNamingStrategy.SnakeCaseStrategy} is applied to the underlying
 * {@link ObjectMapper}.
 * @return a {@link MappingJackson2HttpMessageConverter} object
 */
public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {
	MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
	converter.setObjectMapper(new ObjectMapper()
			.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE));

	SimpleModule jsonModule = new SimpleModule();
	jsonModule.setSerializerModifier(createJsonSerializerModifier()); // keyFormatter,
	// compact));
	converter.getObjectMapper().registerModule(jsonModule);

	converter.getObjectMapper().configure(SerializationFeature.WRAP_ROOT_VALUE, true);
	converter.getObjectMapper().configure(DeserializationFeature.UNWRAP_ROOT_VALUE,
			true);
	converter.getObjectMapper().addMixIn(Applications.class,
			ApplicationsJsonMixIn.class);
	converter.getObjectMapper().addMixIn(InstanceInfo.class,
			InstanceInfoJsonMixIn.class);

	// converter.getObjectMapper().addMixIn(DataCenterInfo.class,
	// DataCenterInfoXmlMixIn.class);
	// converter.getObjectMapper().addMixIn(InstanceInfo.PortWrapper.class,
	// PortWrapperXmlMixIn.class);
	// converter.getObjectMapper().addMixIn(Application.class,
	// ApplicationXmlMixIn.class);
	// converter.getObjectMapper().addMixIn(Applications.class,
	// ApplicationsXmlMixIn.class);

	return converter;
}
 
Example 9
Source File: CustomerClient.java    From microservice-kubernetes with Apache License 2.0 5 votes vote down vote up
protected RestTemplate getRestTemplate() {
	ObjectMapper mapper = new ObjectMapper();
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	mapper.registerModule(new Jackson2HalModule());

	MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
	converter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
	converter.setObjectMapper(mapper);

	return new RestTemplate(Collections.<HttpMessageConverter<?>>singletonList(converter));
}
 
Example 10
Source File: RestTmplApplicationTests.java    From Spring5Tutorial with GNU Lesser General Public License v3.0 5 votes vote down vote up
private MappingJackson2HttpMessageConverter getHalMessageConverter() {
	ObjectMapper objectMapper = new ObjectMapper();
	objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	objectMapper.registerModule(new Jackson2HalModule());
	MappingJackson2HttpMessageConverter halConverter = new TypeConstrainedMappingJackson2HttpMessageConverter(
			ResourceSupport.class);
	halConverter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
	halConverter.setObjectMapper(objectMapper);
	return halConverter;
}
 
Example 11
Source File: CustomerClient.java    From microservice-consul with Apache License 2.0 5 votes vote down vote up
protected RestTemplate getRestTemplate() {
	ObjectMapper mapper = new ObjectMapper();
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
			false);
	mapper.registerModule(new Jackson2HalModule());

	MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
	converter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
	converter.setObjectMapper(mapper);

	return new RestTemplate(
			Collections.<HttpMessageConverter<?>> singletonList(converter));
}
 
Example 12
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    addDefaultHttpMessageConverters(converters);
    for (HttpMessageConverter<?> converter : converters) {
        if (converter instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) converter;
            jackson2HttpMessageConverter.setObjectMapper(objectMapper);
            break;
        }
    }
}
 
Example 13
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    addDefaultHttpMessageConverters(converters);
    for (HttpMessageConverter<?> converter : converters) {
        if (converter instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) converter;
            jackson2HttpMessageConverter.setObjectMapper(objectMapper);
            break;
        }
    }
}
 
Example 14
Source File: DispatcherServletConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    addDefaultHttpMessageConverters(converters);
    for (HttpMessageConverter<?> converter : converters) {
        if (converter instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) converter;
            jackson2HttpMessageConverter.setObjectMapper(objectMapper);
            break;
        }
    }
}
 
Example 15
Source File: NIORestClient.java    From micro-server with Apache License 2.0 5 votes vote down vote up
public NIORestClient(AsyncRestTemplate template) {
	super();

	this.template = template;
	MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
	converter.setObjectMapper(JacksonUtil.getMapper());
	template.getMessageConverters().add(converter);
}
 
Example 16
Source File: ApiDispatcherServletConfiguration.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    addDefaultHttpMessageConverters(converters);
    for (HttpMessageConverter<?> converter: converters) {
        if (converter instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) converter;
            jackson2HttpMessageConverter.setObjectMapper(objectMapper);
            break;
        }
    }
}
 
Example 17
Source File: CaptureTest.java    From riptide with MIT License 4 votes vote down vote up
private MappingJackson2HttpMessageConverter createJsonConverter() {
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(new ObjectMapper().findAndRegisterModules());
    return converter;
}
 
Example 18
Source File: MockSetup.java    From riptide with MIT License 4 votes vote down vote up
private static MappingJackson2HttpMessageConverter defaultJsonConverter() {
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(new ObjectMapper().findAndRegisterModules());
    return converter;
}
 
Example 19
Source File: MockSetup.java    From riptide with MIT License 4 votes vote down vote up
private static MappingJackson2HttpMessageConverter createJsonConverter() {
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(new ObjectMapper().findAndRegisterModules());
    return converter;
}
 
Example 20
Source File: RateLimitResetDelayFunctionTest.java    From riptide with MIT License 4 votes vote down vote up
private static MappingJackson2HttpMessageConverter createJsonConverter() {
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(createObjectMapper());
    return converter;
}