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

The following examples show how to use org.springframework.hateoas.mediatype.hal.Jackson2HalModule. 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: FeignHalAutoConfigurationTests.java    From spring-cloud-openfeign with Apache License 2.0 6 votes vote down vote up
@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 File: DataFlowControllerAutoConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@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 #3
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 #4
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 #5
Source File: ResourceDeserializerTest.java    From bowman with 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 #6
Source File: TaskHistoryEventControllerIntTest.java    From taskana with 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 #7
Source File: CatalogClient.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 #8
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 #9
Source File: CatalogClient.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 #10
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 #11
Source File: DataFlowTemplate.java    From spring-cloud-dataflow with 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 #12
Source File: PageableEntityResultPage.java    From bowman with 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 #13
Source File: DefaultObjectMapperFactory.java    From bowman with 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 #14
Source File: HateoasHalProvider.java    From springdoc-openapi with 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 #15
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 #16
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 #17
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 #18
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 #19
Source File: FeignHalAutoConfiguration.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@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 #20
Source File: DataRestHalProvider.java    From springdoc-openapi with 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());
}