org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter Java Examples

The following examples show how to use org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter. 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: ApiClient.java    From tutorials with MIT License 6 votes vote down vote up
/**
 * Build the RestTemplate used to make HTTP requests.
 * @return RestTemplate
 */
protected RestTemplate buildRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    for(HttpMessageConverter converter:restTemplate.getMessageConverters()){
        if(converter instanceof AbstractJackson2HttpMessageConverter){
            ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
            ThreeTenModule module = new ThreeTenModule();
            module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT);
            module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME);
            module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME);
            mapper.registerModule(module);
            mapper.registerModule(new JsonNullableModule());
        }
    }
    // This allows us to read the response more than once - Necessary for debugging.
    restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
    return restTemplate;
}
 
Example #2
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Build the RestTemplate used to make HTTP requests.
 * @return RestTemplate
 */
protected RestTemplate buildRestTemplate() {
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(new MappingJackson2HttpMessageConverter());
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
    xmlMapper.registerModule(new JsonNullableModule());
    messageConverters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper));

    RestTemplate restTemplate = new RestTemplate(messageConverters);
    
    for(HttpMessageConverter converter:restTemplate.getMessageConverters()){
        if(converter instanceof AbstractJackson2HttpMessageConverter){
            ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
            ThreeTenModule module = new ThreeTenModule();
            module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT);
            module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME);
            module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME);
            mapper.registerModule(module);
            mapper.registerModule(new JsonNullableModule());
        }
    }
    // This allows us to read the response more than once - Necessary for debugging.
    restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
    return restTemplate;
}
 
Example #3
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Build the RestTemplate used to make HTTP requests.
 * @return RestTemplate
 */
protected RestTemplate buildRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    for(HttpMessageConverter converter:restTemplate.getMessageConverters()){
        if(converter instanceof AbstractJackson2HttpMessageConverter){
            ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
            ThreeTenModule module = new ThreeTenModule();
            module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT);
            module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME);
            module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME);
            mapper.registerModule(module);
            mapper.registerModule(new JsonNullableModule());
        }
    }
    // This allows us to read the response more than once - Necessary for debugging.
    restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
    return restTemplate;
}
 
Example #4
Source File: WebMvcConfig.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 配置消息转换器
 * @author Frodez
 * @date 2019-05-10
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	//清除掉原本的AbstractJackson2HttpMessageConverter。
	converters.removeIf((iter) -> {
		return iter instanceof AbstractJackson2HttpMessageConverter;
	});
	converters.add(0, new ResultConverter());
	converters.add(1, new JsonConverter());
}
 
Example #5
Source File: ApiClient.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Set the date format used to parse/format date parameters.
 * @param dateFormat Date format
 * @return API client
 */
public ApiClient setDateFormat(DateFormat dateFormat) {
    this.dateFormat = dateFormat;
    for(HttpMessageConverter converter:restTemplate.getMessageConverters()){
        if(converter instanceof AbstractJackson2HttpMessageConverter){
            ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
            mapper.setDateFormat(dateFormat);
        }
    }
    return this;
}
 
Example #6
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
	List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
	assertEquals(9, converters.size());
	for(HttpMessageConverter<?> converter : converters) {
		if (converter instanceof AbstractJackson2HttpMessageConverter) {
			ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
			assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
			assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
			assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
			if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
				assertEquals(XmlMapper.class, objectMapper.getClass());
			}
		}
	}

	ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
	assertNotNull(initializer);

	ConversionService conversionService = initializer.getConversionService();
	assertNotNull(conversionService);
	assertTrue(conversionService instanceof FormattingConversionService);

	Validator validator = initializer.getValidator();
	assertNotNull(validator);
	assertTrue(validator instanceof LocalValidatorFactoryBean);

	DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
	@SuppressWarnings("unchecked")
	List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
	assertEquals(2, bodyAdvice.size());
	assertEquals(JsonViewRequestBodyAdvice.class, bodyAdvice.get(0).getClass());
	assertEquals(JsonViewResponseBodyAdvice.class, bodyAdvice.get(1).getClass());
}
 
Example #7
Source File: JsonViewRequestBodyAdvice.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supports(MethodParameter methodParameter, Type targetType,
		Class<? extends HttpMessageConverter<?>> converterType) {

	return (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) &&
			methodParameter.getParameterAnnotation(JsonView.class) != null);
}
 
Example #8
Source File: JsonViewRequestBodyAdvice.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean supports(MethodParameter methodParameter, Type targetType,
		Class<? extends HttpMessageConverter<?>> converterType) {

	return (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) &&
			methodParameter.getParameterAnnotation(JsonView.class) != null);
}
 
Example #9
Source File: MessageConfiguration.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 使用 JACKSON 作为JSON MessageConverter
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	converters.removeIf(x -> x instanceof StringHttpMessageConverter || x instanceof AbstractJackson2HttpMessageConverter);
	converters.add(new StringHttpMessageConverter(Charsets.UTF_8));
	converters.add(new ByteArrayHttpMessageConverter());
	converters.add(new ResourceHttpMessageConverter());
	converters.add(new ResourceRegionHttpMessageConverter());
	converters.add(new MappingApiJackson2HttpMessageConverter(objectMapper));
}
 
Example #10
Source File: FilterConfiguration.java    From jfilter with Apache License 2.0 5 votes vote down vote up
/**
 * Add custom converter extemded from AbstractJackson2HttpMessageConverter
 * Note: Message converter should contain FilterObjectMapper or FilterXmlMapper in objectMapper property for correct filtering
 *
 * @param converter instance of message converter extended from {@link AbstractJackson2HttpMessageConverter}
 * @param <T>       {@link AbstractJackson2HttpMessageConverter}
 * @return instance of {@link FilterConfiguration}
 */
public <T extends AbstractJackson2HttpMessageConverter> FilterConfiguration withCustomConverter(T converter) {
    if (converter == null)
        throw new IllegalArgumentException("Converter coudn't be null");

    if (!(converter.getObjectMapper() instanceof FilterObjectMapper) &&
            !(converter.getObjectMapper() instanceof FilterXmlMapper))
        throw new IllegalArgumentException("Converter should contain FilterObjectMapper or FilterXmlMapper in objectMapper property for correct filtering");
    customConverters.add(converter);
    return this;
}
 
Example #11
Source File: JsonViewRequestBodyAdvice.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean supports(MethodParameter methodParameter, Type targetType,
		Class<? extends HttpMessageConverter<?>> converterType) {

	return (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) &&
			methodParameter.getParameterAnnotation(JsonView.class) != null);
}
 
Example #12
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Set the date format used to parse/format date parameters.
 * @param dateFormat Date format
 * @return API client
 */
public ApiClient setDateFormat(DateFormat dateFormat) {
    this.dateFormat = dateFormat;
    for(HttpMessageConverter converter:restTemplate.getMessageConverters()){
        if(converter instanceof AbstractJackson2HttpMessageConverter){
            ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
            mapper.setDateFormat(dateFormat);
        }
    }
    return this;
}
 
Example #13
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Set the date format used to parse/format date parameters.
 * @param dateFormat Date format
 * @return API client
 */
public ApiClient setDateFormat(DateFormat dateFormat) {
    this.dateFormat = dateFormat;
    for(HttpMessageConverter converter:restTemplate.getMessageConverters()){
        if(converter instanceof AbstractJackson2HttpMessageConverter){
            ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
            mapper.setDateFormat(dateFormat);
        }
    }
    return this;
}
 
Example #14
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
	List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
	assertEquals(12, converters.size());
	converters.stream()
			.filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter)
			.forEach(converter -> {
				ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
				assertFalse(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
				assertFalse(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
				assertFalse(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES));
				if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
					assertEquals(XmlMapper.class, mapper.getClass());
				}
			});

	ConfigurableWebBindingInitializer initializer =
			(ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
	assertNotNull(initializer);

	ConversionService conversionService = initializer.getConversionService();
	assertNotNull(conversionService);
	assertTrue(conversionService instanceof FormattingConversionService);

	Validator validator = initializer.getValidator();
	assertNotNull(validator);
	assertTrue(validator instanceof LocalValidatorFactoryBean);

	DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
	@SuppressWarnings("unchecked")
	List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
	assertEquals(2, bodyAdvice.size());
	assertEquals(JsonViewRequestBodyAdvice.class, bodyAdvice.get(0).getClass());
	assertEquals(JsonViewResponseBodyAdvice.class, bodyAdvice.get(1).getClass());
}
 
Example #15
Source File: JsonViewRequestBodyAdvice.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean supports(MethodParameter methodParameter, Type targetType,
		Class<? extends HttpMessageConverter<?>> converterType) {

	return (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) &&
			methodParameter.getParameterAnnotation(JsonView.class) != null);
}
 
Example #16
Source File: ResponseEntityAdvice.java    From mPass with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supports(MethodParameter methodParameter,
                        Class<? extends HttpMessageConverter<?>> aClass) {
    String requeireType = methodParameter.getGenericParameterType().getTypeName();
    return requeireType.startsWith(ResponseEntity.class.getTypeName())
            && AbstractJackson2HttpMessageConverter.class.isAssignableFrom(aClass);
}
 
Example #17
Source File: ResponseEntityAdvice.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supports(MethodParameter methodParameter,
                        Class<? extends HttpMessageConverter<?>> aClass) {
    String requeireType = methodParameter.getGenericParameterType().getTypeName();
    return requeireType.startsWith(ResponseEntity.class.getTypeName())
            && AbstractJackson2HttpMessageConverter.class.isAssignableFrom(aClass);
}
 
Example #18
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void requestMappingHandlerAdapter() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
	List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
	assertEquals(12, converters.size());
	converters.stream()
			.filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter)
			.forEach(converter -> {
				ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
				assertFalse(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
				assertFalse(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
				assertFalse(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES));
				if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
					assertEquals(XmlMapper.class, mapper.getClass());
				}
			});

	ConfigurableWebBindingInitializer initializer =
			(ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
	assertNotNull(initializer);

	ConversionService conversionService = initializer.getConversionService();
	assertNotNull(conversionService);
	assertTrue(conversionService instanceof FormattingConversionService);

	Validator validator = initializer.getValidator();
	assertNotNull(validator);
	assertTrue(validator instanceof LocalValidatorFactoryBean);

	DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
	@SuppressWarnings("unchecked")
	List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
	assertEquals(2, bodyAdvice.size());
	assertEquals(JsonViewRequestBodyAdvice.class, bodyAdvice.get(0).getClass());
	assertEquals(JsonViewResponseBodyAdvice.class, bodyAdvice.get(1).getClass());
}
 
Example #19
Source File: CommonResultControllerAdvice.java    From halo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
    return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
}
 
Example #20
Source File: AbstractMappingJacksonResponseBodyAdvice.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
	return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
}
 
Example #21
Source File: AbstractMappingJacksonResponseBodyAdvice.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
	return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
}
 
Example #22
Source File: AbstractMappingJacksonResponseBodyAdvice.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
	return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
}
 
Example #23
Source File: VaultKeyValueAccessor.java    From spring-vault with Apache License 2.0 4 votes vote down vote up
private static ObjectMapper extractObjectMapper(VaultOperations vaultOperations) {

		Optional<ObjectMapper> mapper = vaultOperations.doWithSession(operations -> {

			if (operations instanceof RestTemplate) {

				RestTemplate template = (RestTemplate) operations;

				Optional<AbstractJackson2HttpMessageConverter> jackson2Converter = template.getMessageConverters()
						.stream().filter(AbstractJackson2HttpMessageConverter.class::isInstance) //
						.map(AbstractJackson2HttpMessageConverter.class::cast) //
						.findFirst();

				return jackson2Converter.map(AbstractJackson2HttpMessageConverter::getObjectMapper);
			}

			return Optional.empty();
		});

		return mapper.orElseGet(ObjectMapper::new);
	}
 
Example #24
Source File: AbstractMappingJacksonResponseBodyAdvice.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
	return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
}