Spring 字段restTemplate需要一个bean,但找到了2个
我正在尝试解决此代码的问题:Spring 字段restTemplate需要一个bean,但找到了2个,spring,spring-boot,spring-security,resttemplate,spring-hateoas,Spring,Spring Boot,Spring Security,Resttemplate,Spring Hateoas,我正在尝试解决此代码的问题: import io.christdoes.wealth.tracker.controller.error.ReCaptchaInvalidException; import io.christdoes.wealth.tracker.controller.error.ReCaptchaUnavailableException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spr
import io.christdoes.wealth.tracker.controller.error.ReCaptchaInvalidException;
import io.christdoes.wealth.tracker.controller.error.ReCaptchaUnavailableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.util.regex.Pattern;
@Service("captchaService")
public class CaptchaService implements ICaptchaService {
private final static Logger LOGGER = LoggerFactory.getLogger(CaptchaService.class);
@Autowired
private HttpServletRequest request;
@Autowired
private CaptchaSettings captchaSettings;
@Autowired
private ReCaptchaAttemptService reCaptchaAttemptService;
@Autowired
private RestOperations restTemplate;
private static final Pattern RESPONSE_PATTERN = Pattern.compile("[A-Za-z0-9_-]+");
@Override
public void processResponse(final String response) {
LOGGER.debug("Attempting to validate response {}", response);
if (reCaptchaAttemptService.isBlocked(getClientIP())) {
throw new ReCaptchaInvalidException("Client exceeded maximum number of failed attempts");
}
if (!responseSanityCheck(response)) {
throw new ReCaptchaInvalidException("Response contains invalid characters");
}
final URI verifyUri = URI.create(String.format("https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s&remoteip=%s", getReCaptchaSecret(), response, getClientIP()));
try {
final GoogleResponse googleResponse = restTemplate.getForObject(verifyUri, GoogleResponse.class);
LOGGER.debug("Google's response: {} ", googleResponse.toString());
if (!googleResponse.isSuccess()) {
if (googleResponse.hasClientError()) {
reCaptchaAttemptService.reCaptchaFailed(getClientIP());
}
throw new ReCaptchaInvalidException("reCaptcha was not successfully validated");
}
} catch (RestClientException rce) {
throw new ReCaptchaUnavailableException("Registration unavailable at this time. Please try again later.", rce);
}
reCaptchaAttemptService.reCaptchaSucceeded(getClientIP());
}
private boolean responseSanityCheck(final String response) {
return StringUtils.hasLength(response) && RESPONSE_PATTERN.matcher(response).matches();
}
@Override
public String getReCaptchaSite() {
return captchaSettings.getSite();
}
@Override
public String getReCaptchaSecret() {
return captchaSettings.getSecret();
}
private String getClientIP() {
final String xfHeader = request.getHeader("X-Forwarded-For");
if (xfHeader == null) {
return request.getRemoteAddr();
}
return xfHeader.split(",")[0];
}
}
错误
我一直在试图解决这个问题,却找不到解决的办法。我不断地得到上面的错误
我搜索了SO和Github,其中报告了类似的错误,但没有得到帮助。有人指出这是一个依赖性问题,既有SpringWeb,也有hateoas可能是问题所在。我删除了web,但问题仍然存在
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot
弹簧靴启动器hateoas
org.springframework.boot
SpringBootStarterWeb
虽然早期的一个项目使用了版本2.0.4
的spring启动依赖项,但我目前使用的是最新版本2.1.8
。我不想恢复到以前的版本,因为我有我单独使用的最新代码2.1.8
我该怎么做才能克服这一挑战?您可以选择以下方法之一:
弹簧引导
,因为它是
spring boot starter hateoas
我认为这会导致两次bean的创建
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
然后,更换自动接线
@Autowired
private RestOperations restTemplate;
作者:
我使用RestTemplateBuilder而不是RestTemplate解决了这个问题。 在我从配置文件中删除@Bean RestTemplate创建之前
private RestTemplate restTemplate;
public MyClass(RestTemplateBuilder restTemplate){
this.restTemplate = restTemplate.build();
}
对此我非常感激。我现在正在使用手机,我会在我的系统上做一次必要的操作。谢谢@Springer F,我已经按照这里的说明做了,但还是一样的。我将尝试清除整个本地maven缓存并重新安装。hi@kehinde,使用第二种方法后也会抛出错误?仍然是相同的错误。这就像是我现在正在拔掉头发一样,因为这些东西已经一天多没出去了。我已经尝试了所有的建议。我删除了本地Maven缓存/存储库并重新运行项目,但同样的情况仍然存在。我在想,可能还是有一些依赖性导致了这个问题。现在我在复习。这些都可能是原因吗?POM文件中有3个:SpringDataJPA、DataRESTCore、DataRESTMVC。我的意见是,在这3个版本中,应该只需要一个版本,如果需要的话,请确定我应该保留和删除哪些版本?尝试删除data rest core和data rest core spring hateoas并恢复spring boot?
@Autowired
private RestOperations restTemplate;
@Autowired
private RestTemplate myRestTemplate;
private RestTemplate restTemplate;
public MyClass(RestTemplateBuilder restTemplate){
this.restTemplate = restTemplate.build();
}