在使用Ribbon調用廣告投放系統API之前,我們需要先建立2個VO對象, AdPlanVO AdPlanGetRequestVO
,
.
//資料請求對象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AdPlanGetRequestVO {
private Long userId;
private List<Long> planIds;
}
----------------------------------
//API請求響應結果反序列化對象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AdPlanVO {
private Long planId;
private Long userId;
private String planName;
private Integer planStatus;
private Date startDate;
private Date endDate;
private Date createTime;
private Date updateTime;
}
在
AdSearchApplication
啟動類中,添加
RestTemplate
用戶端。
public class AdSearchApplication {
...
/**
* 注冊{@link RestTemplate}Bean
* @return
*/
@Bean
@LoadBalanced //讓RestTemplate在調用服務的時候,可以實作負載均衡
RestTemplate restTemplate(){
return new RestTemplate();
}
}
建立一個controller,來測試調用廣告提供系統的API
/**
* SearchController for search information controller
*
* @author <a href="mailto:[email protected]">Isaac.Zhang | 若初</a>
*/
@RestController
@Slf4j
@RequestMapping(path = "/search")
public class SearchController {
//注入RestTemplate
private final RestTemplate restTemplate;
@Autowired
public SearchController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping(path = "/plan/get-ribbon")
public CommonResponse<List<AdPlanVO>> getAdPlansUseRibbon(@RequestBody AdPlanGetRequestVO requestVO) {
log.info("ad-search::getAdPlansUseRibbon -> {}", JSON.toJSONString(requestVO));
return restTemplate.postForEntity(
"http://mscx-ad-sponsor/ad-sponsor/plan/get", requestVO, CommonResponse.class
).getBody();
}
@GetMapping(path = "/user/get")
public CommonResponse getUsers(@Param(value = "username") String username) {
log.info("ad-search::getUsers -> {}", JSON.toJSONString(username));
CommonResponse commonResponse = restTemplate.getForObject(
"http://mscx-ad-sponsor/ad-sponsor/user/get?username={username}", CommonResponse.class, username
);
return commonResponse;
}
}