Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用注释自动关联RestTemplate_Java_Spring_Spring Mvc_Resttemplate - Fatal编程技术网

Java 如何使用注释自动关联RestTemplate

Java 如何使用注释自动关联RestTemplate,java,spring,spring-mvc,resttemplate,Java,Spring,Spring Mvc,Resttemplate,尝试自动关联Spring RestTemplate时,出现以下错误: 嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[org.springframework.web.client.RestTemplate]的符合条件的bean:至少需要1个符合此依赖项autowire候选项条件的bean。 在注释驱动环境中使用Spring 4 我的dispatcher servlet配置如下: <

尝试自动关联Spring RestTemplate时,出现以下错误:

嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到依赖项类型为[org.springframework.web.client.RestTemplate]的符合条件的bean:至少需要1个符合此依赖项autowire候选项条件的bean。

在注释驱动环境中使用Spring 4

我的dispatcher servlet配置如下:

<context:component-scan base-package="in.myproject" />
<mvc:default-servlet-handler />    
<mvc:annotation-driven />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
@Service("httpService")
public class HttpServiceImpl implements HttpService {

@Autowired
private RestTemplate restTemplate;

@Override
public void sendUserId(String userId){

    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("userId", userId);
    map.add("secretKey", "kbhyutu7576465duyfy");

    restTemplate.postForObject("http://localhost:8081/api/user", map, null);


    }
}

我尝试自动关联RestTemplate的类如下所示:

<context:component-scan base-package="in.myproject" />
<mvc:default-servlet-handler />    
<mvc:annotation-driven />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
@Service("httpService")
public class HttpServiceImpl implements HttpService {

@Autowired
private RestTemplate restTemplate;

@Override
public void sendUserId(String userId){

    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("userId", userId);
    map.add("secretKey", "kbhyutu7576465duyfy");

    restTemplate.postForObject("http://localhost:8081/api/user", map, null);


    }
}
@Service(“httpService”)
公共类HttpServiceImpl实现HttpService{
@自动连线
私有RestTemplate RestTemplate;
@凌驾
public void sendUserId(字符串userId){
MultiValueMap=新链接的MultiValueMap();
map.add(“userId”,userId);
地图添加(“秘钥”、“kbhyutu7576465duyfy”);
restTemplate.postForObject(“http://localhost:8081/api/user,映射,空);
}
}

您只能使用autowire接口实现。

如果您使用Spring Boot 1.4.0或更高版本作为注释驱动的基础,Spring不会提供一个自动配置的RESTTemplatebean。根据他们的文件:

如果需要从应用程序调用远程REST服务,可以使用Spring Framework的RestTemplate类。由于RestTemplate实例通常需要在使用之前进行定制,所以SpringBoot不提供任何一个自动配置的RESTTemplatebean。但是,它会自动配置RestTemplateBuilder,可在需要时用于创建RestTemplate实例。自动配置的RestTemplateBuilder将确保将合理的HttpMessageConverters应用于RestTemplate实例


您可以将以下方法添加到类中,以提供RestTemplate的默认实现:

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}
错误如果未定义
RestTemplate
,您将看到该错误

考虑定义类型为的bean 配置中的“org.springframework.web.client.restemplate”

没有类型为的限定bean 找到[org.springframework.web.client.RestTemplate]

如何通过注释定义
RestTemplate

取决于您正在使用的技术以及哪些版本将影响您在
@Configuration
类中定义
RestTemplate
的方式

弹簧>=4,不带弹簧套

只需定义一个
@Bean

@Bean
公共RestTemplate RestTemplate(){
返回新的RestTemplate();
}
弹簧靴=1.4

Spring Boot不再自动定义
RestTemplate
,而是定义了
RestTemplateBuilder
,允许您对创建的
RestTemplate
进行更多控制。您可以将
restemplatebuilder
作为参数插入
@Bean
方法中,以创建
restemplate

@Bean
公共RestTemplate RestTemplate(RestTemplateBuilder){
//在此处执行任何其他配置
返回builder.build();
}
在课堂上使用它


在RestTemplateSOMENAME中添加
@Configuration
注释,该注释扩展了RestTemplate类

@Configuration         
public class RestTemplateClass extends RestTemplate {

}
然后,在控制器类中,可以使用如下自动连线注释

@Autowired   
RestTemplateClass restTemplate;

你能确认你在HttpServiceImpl中导入的是类org.springframework.web.client.RestTemplate而不是其他RestTemplate实现吗?我导入的是同一个org.springframework.web.client.RestTemplate你的配置文件可能没有被读取,在那里添加一些其他bean,看看它是否在上下文中注册,如果不是的话,您在dispatcher-servlet.xml中得到了answerI声明的bean(),而不是applicationContext.xml,现在它工作正常了。rest操作就是他所说的一个接口。若接口有一个实现,你们可以自动连接它。你们确定它能正常工作吗?我得到这个错误:注入点有以下注释:-@ org .prgFrrask.Beas.Futo.Acthuty.AutoWordRead(Value=true):考虑在Office Type + 1中定义“Or.Spring Frask.Web.Client,REST操作”类型的bean,以提及您可以将其添加到类中,不需要把它放在@Configuration annotated classI中,我把它放在我的一个服务中,它说bean创建循环错误,所以我需要用配置注释的另一种方法。问题是在类中自动连接RestTemplate,所以不需要把bean放在
@Configuration
annotated类中。它可以直接放在类中(参见@eaykin答案)。当然,您可以在
@组件
类中声明它(毕竟,
@配置
@组件
的元注释),但是
@配置
是首选。您好,如果我的类是一个函数接口的实现呢?如何自动连接restTemplate对象?存在另一个函数会引发错误!如果您想提供多个rest模板,每个模板的配置都不同,那么这实际上是一个有效的解决方案。虽然我会使用
@Component
装饰而不是
@Configuration
,然后将每个实现作为
@Configuration
中的一个bean提供,但我认为最好使用restemplatebuilder提供多个restemplate bean
@Configuration         
public class RestTemplateClass extends RestTemplate {

}
@Autowired   
RestTemplateClass restTemplate;