从0开始做外卖侠优惠券系统之2.1—将clientId和clientSecret填入配置文件
在上一篇文章中,我们提到过一个clientId和一个clientSecret,两个东西都是写在方法里的,也就是说,以后每写一个方法都需要写这些重复的代码,所以为了提高代码的可读性。我们需要将这些写进配置文件。
1.在application.propertise文件中加入clientId和clientSecret
# 多多客配置信息
pddClientId =
pddClientSecret =
2.在控制器中使用@Value(“${}”)获取变量,然后定义对应私有变量赋值。
public class PopController {
@Value("${pddClientId}")
private String pddClientId;
@Value("${pddClientSecret}")
private String pddClientSecret;
@RequestMapping("/getPddGoodsCats")
public String getPddGoodsCats(@RequestParam int catId){
String clientId = pddClientId;
String clientSecret = pddClientSecret;
PopClient client = new PopHttpClient(clientId, clientSecret);
PddGoodsCatsGetRequest request = new PddGoodsCatsGetRequest();
request.setParentCatId((long) catId);
PddGoodsCatsGetResponse response = null;
try {
response = client.syncInvoke(request);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(JsonUtil.transferToJson(response));
return JsonUtil.transferToJson(response);
}
}
3.然后测试一下,完成。