Java Code Examples for org.springframework.hateoas.mediatype.hal.Jackson2HalModule

The following examples show how to use org.springframework.hateoas.mediatype.hal.Jackson2HalModule. These examples are extracted from open source projects. 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
@Test
public void halJacksonHttpMessageConverter() {
	ObjectMapper mapper = new ObjectMapper();
	when(objectMapper.getIfAvailable(any())).thenReturn(mapper);

	when(halConfiguration.getIfAvailable(any()))
			.thenReturn(mock(HalConfiguration.class));
	when(relProvider.getIfAvailable()).thenReturn(mock(LinkRelationProvider.class));
	when(curieProvider.getIfAvailable(any())).thenReturn(mock(CurieProvider.class));
	when(messageResolver.getIfAvailable()).thenReturn(mock(MessageResolver.class));

	TypeConstrainedMappingJackson2HttpMessageConverter converter = feignHalAutoConfiguration
			.halJacksonHttpMessageConverter(objectMapper, halConfiguration,
					messageResolver, curieProvider, relProvider);

	assertThat(converter).isNotNull();
	assertThat(converter.getObjectMapper()).isNotNull();
	assertThat(converter.getSupportedMediaTypes()).isEqualTo(Arrays.asList(HAL_JSON));

	assertThat(Jackson2HalModule.isAlreadyRegisteredIn(converter.getObjectMapper()))
			.isTrue();
}
 
Example 2
Source Project: taskana   Source File: RestHelper.java    License: 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 3
Source Project: taskana   Source File: RestHelper.java    License: 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 4
Source Project: bowman   Source File: ResourceDeserializerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
	typeResolver = mock(TypeResolver.class);
	configuration = Configuration.build();
	
	instantiator = mock(HandlerInstantiator.class);
	
	doReturn(new ResourceDeserializer(Object.class, typeResolver, configuration))
		.when(instantiator).deserializerInstance(any(), any(), eq(ResourceDeserializer.class));
			
	mapper = new ObjectMapper();
	mapper.setHandlerInstantiator(instantiator);
	mapper.registerModule(new Jackson2HalModule());
	mapper.registerModule(new TestModule());
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	
	doReturn(Object.class).when(typeResolver).resolveType(any(), any(), any());
}
 
Example 5
@Bean
public SkipperClient skipperClient(SkipperClientProperties properties,
		RestTemplateBuilder restTemplateBuilder, ObjectMapper objectMapper,
		@Nullable OAuth2TokenUtilsService oauth2TokenUtilsService) {

	// TODO (Tzolov) review the manual Hal convertion configuration
	objectMapper.registerModule(new Jackson2HalModule());
	objectMapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(
			new AnnotationLinkRelationProvider(), CurieProvider.NONE, MessageResolver.DEFAULTS_ONLY, new HalConfiguration()));
	objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

	RestTemplate restTemplate = restTemplateBuilder
			.errorHandler(new SkipperClientResponseErrorHandler(objectMapper))
			.interceptors(new OAuth2AccessTokenProvidingClientHttpRequestInterceptor(oauth2TokenUtilsService))
			.messageConverters(Arrays.asList(new StringHttpMessageConverter(),
					new MappingJackson2HttpMessageConverter(objectMapper)))
			.build();

	return new DefaultSkipperClient(properties.getServerUri(), restTemplate);
}
 
Example 6
Source Project: springdoc-openapi   Source File: HateoasHalProvider.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Init.
 */
@PostConstruct
protected void init() {
	if (!isHalEnabled())
		return;
	if (!Jackson2HalModule.isAlreadyRegisteredIn(Json.mapper()))
		Json.mapper().registerModule(new Jackson2HalModule());
}
 
Example 7
Source Project: springdoc-openapi   Source File: DataRestHalProvider.java    License: Apache License 2.0 5 votes vote down vote up
@PostConstruct
@Override
protected void init() {
	if (!isHalEnabled())
		return;
	if (!Jackson2HalModule.isAlreadyRegisteredIn(Json.mapper()))
		Json.mapper().registerModule(new Jackson2HalModule());
}
 
Example 8
@Bean
@ConditionalOnMissingBean
public TypeConstrainedMappingJackson2HttpMessageConverter halJacksonHttpMessageConverter(
		ObjectProvider<ObjectMapper> objectMapper,
		ObjectProvider<HalConfiguration> halConfiguration,
		ObjectProvider<MessageResolver> messageResolver,
		ObjectProvider<CurieProvider> curieProvider,
		ObjectProvider<LinkRelationProvider> linkRelationProvider) {

	ObjectMapper mapper = objectMapper.getIfAvailable(ObjectMapper::new).copy();
	mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

	HalConfiguration configuration = halConfiguration
			.getIfAvailable(HalConfiguration::new);

	CurieProvider curieProviderInstance = curieProvider
			.getIfAvailable(() -> new DefaultCurieProvider(Collections.emptyMap()));

	Jackson2HalModule.HalHandlerInstantiator halHandlerInstantiator = new Jackson2HalModule.HalHandlerInstantiator(
			linkRelationProvider.getIfAvailable(), curieProviderInstance,
			messageResolver.getIfAvailable(), configuration);

	mapper.setHandlerInstantiator(halHandlerInstantiator);

	if (!Jackson2HalModule.isAlreadyRegisteredIn(mapper)) {
		Jackson2HalModule halModule = new Jackson2HalModule();
		mapper.registerModule(halModule);
	}

	TypeConstrainedMappingJackson2HttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter(
			RepresentationModel.class);
	converter.setSupportedMediaTypes(Arrays.asList(HAL_JSON));
	converter.setObjectMapper(mapper);
	return converter;
}
 
Example 9
Source Project: microservice-kubernetes   Source File: CustomerClient.java    License: 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 Project: microservice-kubernetes   Source File: CustomerClient.java    License: 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 11
Source Project: microservice-kubernetes   Source File: CatalogClient.java    License: 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 Project: microservice-kubernetes   Source File: CatalogClient.java    License: 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 Project: taskana   Source File: TaskHistoryEventControllerIntTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return a REST template which is capable of dealing with responses in HAL format.
 *
 * @return RestTemplate
 */
private RestTemplate getRestTemplate() {
  ObjectMapper mapper = new ObjectMapper();
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  mapper.registerModule(new Jackson2HalModule());

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

  return new RestTemplate(Collections.singletonList(converter));
}
 
Example 14
Source Project: bowman   Source File: DefaultObjectMapperFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ObjectMapper create(HandlerInstantiator instantiator) {
	ObjectMapper mapper = new ObjectMapper();
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	mapper.registerModule(new Jackson2HalModule());
	mapper.registerModule(new JacksonClientModule());
	mapper.setHandlerInstantiator(instantiator);
	return mapper;
}
 
Example 15
Source Project: bowman   Source File: PageableEntityResultPage.java    License: Apache License 2.0 5 votes vote down vote up
@JsonProperty("_embedded")
@JsonDeserialize(
	using = Jackson2HalModule.HalResourcesDeserializer.class,
	contentUsing = InlineAssociationDeserializer.class,
	contentAs = PageableEntity.class)
public List<PageableEntity> getContent() {
	return content;
}
 
Example 16
Source Project: spring-cloud-dataflow   Source File: DataFlowTemplate.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Mutable operation to add several required MixIns to the provided
 * {@link ObjectMapper}.
 *
 * @param objectMapper Must not be null
 * @return ObjectMapper with several mixIns applied
 */
public static ObjectMapper prepareObjectMapper(ObjectMapper objectMapper) {
	Assert.notNull(objectMapper, "The objectMapper must not be null.");
	return objectMapper
		.registerModule(new Jackson2HalModule())
		.addMixIn(JobExecution.class, JobExecutionJacksonMixIn.class)
		.addMixIn(JobParameters.class, JobParametersJacksonMixIn.class)
		.addMixIn(JobParameter.class, JobParameterJacksonMixIn.class)
		.addMixIn(JobInstance.class, JobInstanceJacksonMixIn.class)
		.addMixIn(ExitStatus.class, ExitStatusJacksonMixIn.class)
		.addMixIn(StepExecution.class, StepExecutionJacksonMixIn.class)
		.addMixIn(ExecutionContext.class, ExecutionContextJacksonMixIn.class)
		.addMixIn(StepExecutionHistory.class, StepExecutionHistoryJacksonMixIn.class);
}
 
Example 17
Source Project: microservice   Source File: CustomerClient.java    License: 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 18
Source Project: microservice   Source File: CatalogClient.java    License: 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 19
Source Project: microservice   Source File: CustomerClient.java    License: 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 Project: microservice   Source File: CatalogClient.java    License: 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));
}