Spring HATEOS:考虑在您的配置中定义类型为 'org.springframework.hateoas.config.HypermediaRestTemplateConfigurer' 的 bean
Spring HATEOS : Consider defining a bean of type 'org.springframework.hateoas.config.HypermediaRestTemplateConfigurer' in your configuration
我正在尝试使用 Spring HATEOS 库构建一个简单的 REST 客户端。
我在尝试使用 HypermediaRestTemplateConfigurer
注册我的 RestTemplate
时遇到以下异常
**************************
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.
问题: 如何自己创建一个HypermediaRestTemplateConfigurer
实例?我假设这个 class 会根据示例文档由 spring 引导自动配置?
我的代码:
主要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);
};
}
}
配置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>
解决方案是在我的 @Configuration
注释 class 上方使用 @EnableHypermediaSupport(type = HypermediaType.HAL)
。 (即 RestClientConfiguraiton
class 在我的问题中显示)
幸运的是 HypermediaAutoConfiguration Java 文档中提到了这一点:
Auto-configuration for Spring HATEOAS's @EnableHypermediaSupport.
我正在尝试使用 Spring HATEOS 库构建一个简单的 REST 客户端。
我在尝试使用 HypermediaRestTemplateConfigurer
RestTemplate
时遇到以下异常
**************************
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.
问题: 如何自己创建一个HypermediaRestTemplateConfigurer
实例?我假设这个 class 会根据示例文档由 spring 引导自动配置?
我的代码:
主要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);
};
}
}
配置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>
解决方案是在我的 @Configuration
注释 class 上方使用 @EnableHypermediaSupport(type = HypermediaType.HAL)
。 (即 RestClientConfiguraiton
class 在我的问题中显示)
幸运的是 HypermediaAutoConfiguration Java 文档中提到了这一点:
Auto-configuration for Spring HATEOAS's @EnableHypermediaSupport.