您好,登錄后才能下訂單哦!
在使用Ribbon調(diào)用廣告投放系統(tǒng)API之前,我們需要先創(chuàng)建2個(gè)VO對(duì)象,AdPlanVO
,AdPlanGetRequestVO
.
//數(shù)據(jù)請(qǐng)求對(duì)象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AdPlanGetRequestVO {
private Long userId;
private List<Long> planIds;
}
----------------------------------
//API請(qǐng)求響應(yīng)結(jié)果反序列化對(duì)象
@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
啟動(dòng)類中,添加RestTemplate
客戶端。
public class AdSearchApplication {
...
/**
* 注冊(cè){@link RestTemplate}Bean
* @return
*/
@Bean
@LoadBalanced //讓RestTemplate在調(diào)用服務(wù)的時(shí)候,可以實(shí)現(xiàn)負(fù)載均衡
RestTemplate restTemplate(){
return new RestTemplate();
}
}
創(chuàng)建一個(gè)controller,來測(cè)試調(diào)用廣告提供系統(tǒng)的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;
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。