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

The following examples show how to use org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#setSupportedMediaTypes() . 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: WebConfig.java    From cf-SpringBootTrader with Apache License 2.0 6 votes vote down vote up
/**
 * configure the message converters with the date formatter.
 */
@Override
public void configureMessageConverters(
		List<HttpMessageConverter<?>> converters) {
	// Configure JSON support
	MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
	mappingJacksonHttpMessageConverter.setSupportedMediaTypes(Arrays
			.asList(MediaType.APPLICATION_JSON));
	//mappingJacksonHttpMessageConverter.getObjectMapper().configure(
	//		Feature.WRITE_DATES_AS_TIMESTAMPS, true);
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
	// There is no need to set the timezone as Jackson uses GMT and not the
	// local time zone (which is exactly what you want)
	// Note: While SimpleDateFormat is not threadsafe, Jackson Marshaller's
	// StdSerializerProvider clones the configured formatter for each thread
	mappingJacksonHttpMessageConverter.getObjectMapper().setDateFormat(
			format);
	//mappingJacksonHttpMessageConverter.getObjectMapper().configure(
	//		Feature.INDENT_OUTPUT, true);
	// mappingJacksonHttpMessageConverter.getObjectMapper().getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
	converters.add(mappingJacksonHttpMessageConverter);
}
 
Example 2
Source File: MyfeedAutoConfig.java    From myfeed with Apache License 2.0 6 votes vote down vote up
private List<HttpMessageConverter<?>> getHttpMessageConverters() {
	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));

	ObjectMapper mapper = new ObjectMapper();
	mapper.registerModule(new Jackson2HalModule());
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

	MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

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

	converters.add(converter);
	return converters;
}
 
Example 3
Source File: SecurityConfig.java    From springboot-security-wechat with Apache License 2.0 6 votes vote down vote up
@Bean
public Filter ssoFilter(ClientResources client, String path) {
    MappingJackson2HttpMessageConverter customJsonMessageConverter = new
            MappingJackson2HttpMessageConverter();
    customJsonMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN));
    MyOAuth2ClientAuthenticationProcessingFilter filter = new MyOAuth2ClientAuthenticationProcessingFilter(path);
    filter.setAllowSessionCreation(true);
    MyOAuth2RestTemplate template = new MyOAuth2RestTemplate(client.getClient(), oauth2ClientContext);
    template.setMessageConverters(Arrays.asList(customJsonMessageConverter));
    filter.setRestTemplate(template);
    MyUserInfoTokenServices tokenServices = new MyUserInfoTokenServices(client.getResource().getUserInfoUri(),
            client.getClient().getClientId(),
            userService,
            userWechatService,
            userPrivilegeService,
            privilegeConfigService);
    tokenServices.setRestTemplate(template);
    filter.setTokenServices(tokenServices);
    return filter;
}
 
Example 4
Source File: Application.java    From spring-cloud-microservice-example with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    jsonConverter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    objectMapper.registerModule(new Jackson2HalModule());
    jsonConverter.setObjectMapper(objectMapper);
    return jsonConverter;
}
 
Example 5
Source File: HttpClientConfig.java    From wecube-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void customize(RestTemplate restTemplate) {
    restTemplate.setRequestFactory(clientHttpRequestFactory());
    restTemplate.getInterceptors().add(new CustomClientHttpRequestInterceptor());
    // TODO: please remove this custom converter when upstream system (eg.CMDB)
    // refactor all stupid content-type 'text/json;charset=utf-8' to
    // 'application/json'
    MappingJackson2HttpMessageConverter textJsonConverter = new MappingJackson2HttpMessageConverter();
    textJsonConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "*")));
    restTemplate.getMessageConverters().add(textJsonConverter);
}
 
Example 6
Source File: CtripMQService.java    From apollo with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
  restTemplate = new RestTemplate();

  SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
  rf.setReadTimeout(portalConfig.readTimeout());
  rf.setConnectTimeout(portalConfig.connectTimeout());

  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setSupportedMediaTypes(
      Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_OCTET_STREAM));

  restTemplate.setMessageConverters(Arrays.asList(converter, new FormHttpMessageConverter()));

}
 
Example 7
Source File: MvcAutoConfiguration.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
/**
 * mappingJacksonHttpMessageConverter .
 * @return mappingJacksonHttpMessageConverter
 */
@Bean (name = "mappingJacksonHttpMessageConverter")
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter = 
            new MappingJackson2HttpMessageConverter();
    ArrayList<MediaType> mediaTypesList = new ArrayList<MediaType>();
    mediaTypesList.add(MediaType.APPLICATION_JSON);
    //mediaTypesList.add(MediaType.TEXT_PLAIN);
    mappingJacksonHttpMessageConverter.setSupportedMediaTypes(mediaTypesList);
    return mappingJacksonHttpMessageConverter;
}
 
Example 8
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 9
Source File: TaskanaWildflyTest.java    From taskana with Apache License 2.0 5 votes vote down vote up
private RestTemplate getRestTemplate() {
  ObjectMapper mapper = new ObjectMapper();
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/json"));
  converter.setObjectMapper(mapper);

  return new RestTemplate(Collections.singletonList(converter));
}
 
Example 10
Source File: AuthenticatedRestTemplate.java    From mojito with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a customized {@link MappingJackson2HttpMessageConverter} to process
 * payload from TMS Rest API.
 *
 * @return
 */
protected void makeRestTemplateWithCustomObjectMapper(RestTemplate restTemplate) {
    logger.debug("Getting new rest template");

    for (HttpMessageConverter<?> httpMessageConverter : restTemplate.getMessageConverters()) {
        if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) httpMessageConverter;

            // Bug with application/x-spring-data-compact+json: https://jira.spring.io/browse/DATAREST-404
            logger.debug("Setting supported media type to just JSON. The Accept header will be updated accordingly to application/json");
            mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));

            // This helps with joda deserialize of joda date time string
            logger.debug("Creating custom jackson2 objectmapper with serialization inclusion changes");
            Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean = new Jackson2ObjectMapperFactoryBean();
            jackson2ObjectMapperFactoryBean.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            jackson2ObjectMapperFactoryBean.afterPropertiesSet();

            ObjectMapper objectMapper = jackson2ObjectMapperFactoryBean.getObject();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

            mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
        }

        // The default encoding is set to ISO-8559-1 for String type, which is why we have to override it here
        // For more info: https://jira.spring.io/browse/SPR-9099
        //TODO investigate but this should probalby replaced with setting the right header: headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        if (httpMessageConverter instanceof StringHttpMessageConverter) {
            StringHttpMessageConverter stringHttpMessageConverter = (StringHttpMessageConverter) httpMessageConverter;
            stringHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
                            MediaType.parseMediaType("text/plain;charset=UTF-8"),
                            MediaType.ALL)
            );
        }
    }
}
 
Example 11
Source File: WebMvcConfig.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    log.info("Configuring http message converters...");
    MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
    List<MediaType> types = new ArrayList<>(1);
    types.add(ApplicationMediaType.APPLICATION_JSON);
    types.add(ApplicationMediaType.APPLICATION_BEARCHOKE_V1_JSON);
    types.add(ApplicationMediaType.APPLICATION_BEARCHOKE_V2_JSON);
    jacksonConverter.setSupportedMediaTypes(types);
    jacksonConverter.setObjectMapper(objectMapper);
    converters.add(jacksonConverter);
}
 
Example 12
Source File: CatalogClient.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 13
Source File: CatalogClient.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 14
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 15
Source File: HalRestTemplate.java    From moserp with Apache License 2.0 5 votes vote down vote up
public HalRestTemplate() {
    ObjectMapper halObjectMapper = new ObjectMapper();
    halObjectMapper.registerModule(new Jackson2HalModule());
    halObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    MappingJackson2HttpMessageConverter halConverter = new MappingJackson2HttpMessageConverter();
    halConverter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
    halConverter.setObjectMapper(halObjectMapper);
    getMessageConverters().add(halConverter);
}
 
Example 16
Source File: WebMvcConfiguration.java    From bonita-ui-designer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Spring MVC use a default objectMapper. Objects passed to and returned from the controllers are converted to and from HTTP messages by HttpMessageConverter
 * instances. We must use our {{@link #objectMapper}} because of the subtypes.... So we declare two message converters
 * <ul>
 * <li>StringHttpMessageConverter to format the String sent by HTTP like a JSON object representation</li>
 * <li>MappingJackson2HttpMessageConverter to use our {{@link #objectMapper}}</li>
 * </ul>To declare a JacksonHttpMessageConvet
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //Add a converter for the String sent via HTTP
    StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
    stringConverter.setWriteAcceptCharset(false);  // see SPR-7316
    converters.add(stringConverter);

    //Use our custom Jackson serializer
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(objectMapper);
    mappingJackson2HttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes());
    converters.add(mappingJackson2HttpMessageConverter);

}
 
Example 17
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 18
Source File: AsyncGetService.java    From orders with Apache License 2.0 5 votes vote down vote up
@Autowired
public AsyncGetService(RestProxyTemplate restProxyTemplate) {
    this.restProxyTemplate = restProxyTemplate;
    this.halTemplate = new RestTemplate(restProxyTemplate.getRestTemplate().getRequestFactory());

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.registerModule(new Jackson2HalModule());
    MappingJackson2HttpMessageConverter halConverter = new MappingJackson2HttpMessageConverter();
    halConverter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
    halConverter.setObjectMapper(objectMapper);
    halTemplate.setMessageConverters(Collections.singletonList(halConverter));
}
 
Example 19
Source File: CustomerClient.java    From microservice 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 20
Source File: CatalogClient.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));
}