在使用@Component
注解的工具类中,注入RestTemplate
和读取nacos配置@Value
注解读取失败。解决办法是使用@PostConstruct
注解进行初始化。
@Component
public class SmsUtil {
private static SmsUtil smsUtil;
private static RestTemplate restTemplate;
private static String baseUrl;
private static String sendOrBalanceUrl;
private static String statusUrl;
private static String modifyPwdUrl;
private static String userid;
private static String account;
private static String password;
private static String isEnable;
@PostConstruct
public void init() {
smsUtil = this;
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
@Autowired
public void setRestTemplate(RestTemplate restTemplate) {
SmsUtil.restTemplate = restTemplate;
}
@Value("${sms.baseUrl}")
public void setBaseUrl(String baseUrl) {
SmsUtil.baseUrl = baseUrl;
}
@Value("${sms.sendOrBalanceUrl}")
public void setSendOrBalanceUrl(String sendOrBalanceUrl) {
SmsUtil.sendOrBalanceUrl = sendOrBalanceUrl;
}
@Value("${sms.statusUrl}")
public void setStatusUrl(String statusUrl) {
SmsUtil.statusUrl = statusUrl;
}
@Value("${sms.modifyPwdUrl}")
public void setModifyPwdUrl(String modifyPwdUrl) {
SmsUtil.modifyPwdUrl = modifyPwdUrl;
}
@Value("${sms.userid}")
public void setUserid(String userid) {
SmsUtil.userid = userid;
}
@Value("${sms.account}")
public void setAccount(String account) {
SmsUtil.account = account;
}
@Value("${sms.password}")
public void setPassword(String password) {
SmsUtil.password = password;
}
@Value("${sms.isEnable}")
public void setIsEnable(String isEnable) {
SmsUtil.isEnable = isEnable;
}
private JSONObject restTemplateHttpSend(String url, String param) {
JSONObject resObject = null;
try {
ResponseEntity<String> responseEntity = restTemplate.exchange(baseUrl + url + param, HttpMethod.POST, null, String.class);
HttpStatus statusCode = responseEntity.getStatusCode();
if (statusCode == HttpStatus.OK) {
log.info(">>>http返回结果:", responseEntity.getBody());
resObject = JSON.parseObject(responseEntity.getBody());
}
} catch (Exception e) {
e.printStackTrace();
}
return resObject;
}
}
References:
最新评论
网飞没问题, 迪士尼+有解决方案么?
pp助手是安卓手机用的,根本下载用不来苹果
已解决
这样的话数据库里的结构为{"attachment":{"content":"xxx"}}, 要怎么才能变成{"content":"xxx"},从而使结构保持一致?
赞! make test不过的坑都写到的,谢谢楼主~
谢谢你
用了root用户还是一直502是怎么回事呢
student id 是空的