淘淘商城15_查询分类信息,发布rest服务

2 篇文章 0 订阅
2 篇文章 0 订阅

1. json格式分析

想让taotao-portal这个工程调用taotao-rest工程方法,返回一个规定好的json格式的数据.

在taotao-rest工程中,查询tb_item_cat这张表,拼一个json格式出来.

2.  数据拼装

外层:data  是一个list集合,所以封装一个返回的类CateResult

package com.taotao.rest.pojo;

import java.util.List;

public class CateResult {
	
	private List<?> data;

	public List<?> getData() {
		return data;
	}

	public void setData(List<?> data) {
		this.data = data;
	}
	
	
}

里层:u 是字符串 n 是字符串 i 是list集合

所以封装一个CateNode

package com.taotao.rest.pojo;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

public class CateNode {
	
	@JsonProperty("u")//跟category.json里面"u"对应上
	private String url;
	@JsonProperty("n")//跟category.json里面"n"对应上
	private String name;
	@JsonProperty("i")//跟category.json里面"i"对应上
	private List<?> item;
	
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<?> getItem() {
		return item;
	}
	public void setItem(List<?> item) {
		this.item = item;
	}
	
	
}

3. service

package com.taotao.rest.service;

import com.taotao.rest.pojo.CateResult;

public interface ItemCateService {
	//查询分类表
	public CateResult getItemCateList();
}

4. serviceImpl

package com.taotao.rest.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.mapper.ItemCateMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.rest.pojo.CateNode;
import com.taotao.rest.pojo.CateResult;
import com.taotao.rest.service.ItemCateService;

@Service
public class ItemCateServiceImpl implements ItemCateService {

	@Autowired
	private ItemCateMapper itemCateMapper;
	
	
	@Override
	public CateResult getItemCateList() {
		CateResult result = new CateResult();
		result.setData(getItemCate(0));//给初始值0即可
		return result;
	}
	
	public List getItemCate(long parentId){
		List<TbItemCat> list = itemCateMapper.getItemCateList();
		List resultList = new ArrayList<>();
		for (TbItemCat tbItemCat : list) {
			if (tbItemCat.getIsParent()) {
				CateNode node = new CateNode();
				if (parentId==0) {
					node.setName("<a href='/products/"+tbItemCat.getId()+".html'>"+tbItemCat.getName()+"</a>");
				} else {
					node.setName(tbItemCat.getName());
				}
				node.setUrl("/products/"+tbItemCat.getId()+".html");
				node.setItem(getItemCate(tbItemCat.getId()));
				resultList.add(node);
			} else {
				resultList.add("/products/"+tbItemCat.getId()+".html|"+tbItemCat.getName()+"");
			}
		}
		return resultList;
	}

}

5. Controller

package com.taotao.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.rest.pojo.CateResult;
import com.taotao.rest.service.ItemCateService;
import com.taotao.utils.JsonUtils;

@Controller
@RequestMapping("/itemCate")
public class ItemCateController {

	@Autowired
	private ItemCateService itemCateService;
	
	/**
	 * 查询返回数据,调用service里面的方法
	 * @param callback
	 * @return
	 */
	/*@RequestMapping(value="/list", produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8;")
	@ResponseBody
	public String getItemCate(String callback){
		CateResult result = itemCateService.getItemCateList();
		//转换为json格式
		String jsonData = JsonUtils.objectToJson(result);
		//拼回调函数
		String jsonResult = callback+"("+jsonData+");";//按照category.json里面拼
		return jsonResult;
	}*/
	
	/**
	 * 这种方法也可以,等同于上面的效果
	 * @param callback
	 * @return
	 */
	@RequestMapping(value="/list", produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8;")
	@ResponseBody
	public Object getItemCate(String callback){
		CateResult result = itemCateService.getItemCateList();
		MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
		mappingJacksonValue.setJsonpFunction(callback);
		return mappingJacksonValue;
	}
}

6. 测试结果

有乱码显示,修改Controller

package com.taotao.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.rest.pojo.CateResult;
import com.taotao.rest.service.ItemCateService;
import com.taotao.utils.JsonUtils;

@Controller
@RequestMapping("/itemCate")
public class ItemCateController {

	@Autowired
	private ItemCateService itemCateService;
	
	/**
	 * 查询返回数据,调用service里面的方法
	 * @param callback
	 * @return
	 */
	@RequestMapping(value="/list", produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8;")
	@ResponseBody
	public String getItemCate(String callback){
		CateResult result = itemCateService.getItemCateList();
		//转换为json格式
		String jsonData = JsonUtils.objectToJson(result);
		//拼回调函数
		String jsonResult = callback+"("+jsonData+");";//按照category.json里面拼
		return jsonResult;
	}
}

重新启动,测试结果: 

 

7. 修改taotao-portal工程,用portal工程调用rest工程

修改js里面的lib-v1.js

 

页面效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值