I am trying to build a simple REST client using the Spring HATEOS library.
I get the following exception when trying to register my RestTemplate
with a HypermediaRestTemplateConfigurer
**************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method hypermediaRestTemplateCustomizer in com.rest.client.config.RestClientConfiguraiton required a bean of type 'org.springframework.hateoas.config.HypermediaRestTemplateConfigurer' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.hateoas.config.HypermediaRestTemplateConfigurer' in your configuration.
Question : How do I create a HypermediaRestTemplateConfigurer
instance myself? I assumed this class would have been autoconfigured by spring boot as per the example documentation?
My code :
Main class
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class RestClientApplication {
public static void main(String[] args) {
SpringApplication.run(RestClientApplication .class, args);
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
String taskExecutionResources = restTemplate.getForObject(
"http://mydataflow-server.myhost.net/tasks/executions?name=task1225",
String.class);
System.out.println(taskExecutionResources);
};
}
}
Configuration class
import java.time.Duration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.config.HypermediaRestTemplateConfigurer;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestClientConfiguraiton {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.setConnectTimeout(Duration.ofMinutes(5)).build();
}
@Bean
public RestTemplateCustomizer hypermediaRestTemplateCustomizer(HypermediaRestTemplateConfigurer configurer) {
return restTemplate -> {
configurer.registerHypermediaTypes(restTemplate);
};
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.myapp</groupId>
<artifactId>rest-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest-client</name>
<description>Rest client for SCDF server</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
<exclusion>
<artifactId>tomcat-embed-core</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
<exclusion>
<artifactId>tomcat-embed-websocket</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The solution was to use @EnableHypermediaSupport(type = HypermediaType.HAL)
above my @Configuration
annotated class. (i.e for RestClientConfiguraiton
class show in my question)
Luckily there was a mention of this in HypermediaAutoConfiguration Java Docs :
Auto-configuration for Spring HATEOAS's @EnableHypermediaSupport.