-
Notifications
You must be signed in to change notification settings - Fork 472
Simplify configuration setup #719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
We now avoid to register ObjectMapper instances as Spring beans and rather use one already existing in the ApplicationContext and copying the setup before registering the individual HttpMessageConverters for the individual media types. Replaced a lot of the programmatic component setup via BeanDefinitions with their corresponding JavaConfig alternatives. Removed obsolete media type specific HttpMessageConverters and configuration classes registering them. Backport of fix for #719.
We now avoid to register ObjectMapper instances as Spring beans and rather use one already existing in the ApplicationContext and copying the setup before registering the individual HttpMessageConverters for the individual media types. Replaced a lot of the programmatic component setup via BeanDefinitions with their corresponding JavaConfig alternatives. Removed obsolete media type specific HttpMessageConverters and configuration classes registering them.
To summarize the changes made:
|
I gave it a try (Spring Boot 2.0.4.RELEASE including Spring HATEOAS 0.25.0.RELEASE) and found that Spring HATEOAS does not work in controller tests anymore. It seems that App @SpringBootApplication
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
} Controller @RestController
@RequestMapping("/")
public class IndexController {
@GetMapping
public ResponseEntity<ResourceSupport> index() {
ResourceSupport index = new ResourceSupport();
index.add(linkTo(methodOn(IndexController.class).index()).withSelfRel());
return ResponseEntity.ok().body(index);
}
} Test @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class IndexControllerTest {
@Autowired private TestRestTemplate clientRestTemplate;
@Test
public void responseShouldContainLinks() {
ResponseEntity<ResourceSupport> response = clientRestTemplate.getForEntity("/", ResourceSupport.class);
// ↓↓ This assertion fails
assertThat(response.getBody().getLink("self")).isNotNull();
}
} Full sample project: https://github.com/sbley/spring-boot-2-hateoas |
It looks like Paging @wilkinsona as I am not familiar with the internals of how |
The |
That would be the problem, then. Spring HATEOAS's HateoasConfiguration has: @Bean
ConverterRegisteringBeanPostProcessor jackson2ModuleRegisteringBeanPostProcessor(
ObjectFactory<ConverterRegisteringWebMvcConfigurer> configurer) {
return new ConverterRegisteringBeanPostProcessor(configurer);
} This BPP in turn creates a In its present configuration, those converters never get registered with the context. |
I dug a bit deeper and I can't quite find the conceptual difference to 0.24.0.RELEASE as the media-type specific |
Just to let you know: Spring Boot 2.0.5.RELEASE together with downgraded Spring HATEOAS 0.24.0.RELEASE works. |
You can work around the problem by adding the following bean to your application (and only using @Bean
public RestTemplateCustomizer halCustomizer(ConverterRegisteringWebMvcConfigurer customizer) {
return (template) -> customizer.extendMessageConverters(template.getMessageConverters());
} |
Yeah, I think that did the trick. Thanks, @wilkinsona |
Sorry for commenting on a closed ticket. But isn't the obvious problem here is that |
There have been quite a few tickets (#333, spring-projects/spring-boot#8174) that make obvious that the current configuration step is quite complex and creates difficulties for downstream projects to properly configure especially the Jackson
ObjectMapper
to use.There are a couple of areas we can improve on:
ObjectMapper
at all. Instead, we should rather lazily refer to a uniqueObjectMapper
instance in theApplicationContext
and derive the ones to be used inside the media type specificHttpMessageConverters
from that one.BeanDefinition
instances programmatically.The text was updated successfully, but these errors were encountered: