嗨,我有这个项目在Spring Boot上运行。当我尝试运行时,它给我以下错误:
说明:
com.mycompany.microservice.myproject.bot.controller.BotCommandController中的字段clientCredentialsApi需要一个类型为'org.springframework.web.client.RestOperations'的bean。
操作:
考虑在您的配置中定义类型为“ org.springframework.web.client.RestOperations”的bean。
这是我的代码:
Application.java
package com.mycompany.microservice.myproject
//some imports
@SpringBootApplication`
@ComponentScan(basePackages = "com.mycompany.*")
public class Application {
public static void main(String[] args) {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这是控制器:
BotCommandController.java
package com.mycompany.microservice.myproject.bot.controller;
//some imports
@RestController
@RequestMapping("/bot-command")
public class BotCommandController {
@Autowired
private RestOperations clientCredentialsApi;
@RequestMapping(value = "/sraix", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String sraixCommand(@RequestParam(name = "input", required = false) final String input,@RequestParam(name = "cs", required = false) final String cs) throws Exception {
final UserApiObject userApiObject = clientCredentialsApi.getForObject(env.getProperty("gms.location") + "/rest/user/" + userId, UserApiObject.class);
return userApiObject.getRole();
}
答案 0 :(得分:0)
您正在尝试自动装配未定义或实现的bean。 RestOperations
是一个接口,必须实现。
@Autowired
private RestOperations clientCredentialsApi;
Spring正在寻找用@Bean或@Component或@Service注释的类以注入其引用。在您的情况下,您没有定义bean RestOperations,而Spring无法将其注入BotCommandController。
类似这样的东西:
@Bean
RestOperations rest(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.basicAuthorization("user", "password").build();
}