RestTemplate注入失敗

異常:

Description:

Field restTemplate in com.lch.myairpan.login.LoginController required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

原因分析:網上說這個玩意兒不能直接注入 ,需要些配置類,寫完配置類後,可以正常啓動服務,but 用eureka發請求,報錯未知的主機名。具體原因未知(估計是配置類寫的不對,這個還要好好研究一下)。

解決辦法:

在啓動類加註解配置,通過註解初始化bean 順利通過eureka發現服務

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章