您好,登錄后才能下訂單哦!
在使用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:magicianisaac@gmail.com">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;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。