实现效果:中间大广告显示
实现前端系统获取后端系统提供的接口。zheyi-portal调用zheyi-rest调用zheyi-manager。
Rest层
在zheyi-rest工程下创建。
Service:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@Service
public class ContentServiceImpl implements ContentService {
	
	@Autowired
	private TbContentMapper contentMapper;
	@Override
	public List<TbContent> getContentList(long contentCategoryId) {
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(contentCategoryId);
		
		List<TbContent> result = contentMapper.selectByExample(example);
		return result;
	}
}
Controller:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16@Controller
@RequestMapping("/content")
public class ContentServiceController {
	
	@Autowired
	private ContentService contentService;
	
	@RequestMapping("/list/{contentCategoryId}")
	@ResponseBody
	public TaotaoResult getContentList(@PathVariable long contentCategoryId){
		List<TbContent> result = contentService.getContentList(contentCategoryId);
		
		return TaotaoResult.ok(result);
	}
}
httpclient
HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。
官网: httpclient
Frontend层
在zheyi-portal,需要使用httpclient调用taotao-rest的服务。
Service:
返回一个json字符串。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34@Service
public class ContentServiceImpl implements ContentService {
	
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${REST_INDEX_AD_URL}")
	private String REST_INDEX_AD_URL;
	@Override
	public String getContentList() {
		String result = HttpClientUtil.doGet(REST_BASE_URL + REST_INDEX_AD_URL);
		TaotaoResult taotaoResult = TaotaoResult.formatToList(result, TbContent.class);
		//取内容列表
		List<TbContent> list = (List<TbContent>) taotaoResult.getData();
		List<Map> resultList = new ArrayList<>();
			//创建一个jsp页码要求的pojo列表
		for (TbContent tbContent : list) {
			Map map = new HashMap<>();
			map.put("src", tbContent.getPic());
			map.put("height", 240);
			map.put("width", 670);
			map.put("srcB", tbContent.getPic2());
			map.put("widthB", 550);
			map.put("heightB", 240);
			map.put("href", tbContent.getUrl());
			map.put("alt", tbContent.getSubTitle());
			resultList.add(map);
		}
		return JsonUtils.objectToJson(resultList);
	}
}
Controller:
返回一个逻辑视图。1
2
3
4
5
6
7
8
9
10
11
12
13
14@Controller
public class HomeConrtoller {
	
	@Autowired
	private ContentService contentService;
	
	@RequestMapping("/index")
	public String home(Model model){
		String adJson = contentService.getContentList();
		model.addAttribute("ad1", adJson);
		return "index";
	}
}