Commodity information service release
need
Provide three interfaces, respectively provide basic product information, product description, product parameters, and set the cache time to 1 day.
Developed in taotao-rest.
Basic product information:
Request url: /item/base/{itemId}
Request parameter: product id in url
Response information: TaoTaoResult, which contains the basic information of the product.
product description:
Request url: /item/desc/{itemId}
Request parameter: product id in url
Response information: TaoTaoResult, including product description.
Basic product information:
Request url: /item/param/{itemId}
Request parameter: product id in url
Response information: TaoTaoResult, including product parameters.
Service layer
Set redis related parameters in redis.properties.
The key format of product basic information cached in redis is: REDIS_ITEM_KEY: product id: ITEM_BASE_KEY.
This key separated by: can represent the data in redis hierarchically, and make up for the defect that the hash type cannot set the time of the state-owned enterprise.
Product description and parameter key format are similar.
#商品的key
REDIS_ITEM_KEY=item
#商品基础信息的key
ITEM_BASE_KEY=base
#商品描述的key
ITEM_DESC_KEY=desc
#商品参数的key
ITEM_PARAM_KEY=param
#商品过期时间
REDIS_ITEM_EXPIRE=86400
Create ItemService and the corresponding implementation class, obtain the basic information, description, and parameters of the product according to the id, and use TaoTaoResult to wrap it.
Pay attention to the read and write of the cache, and the setting of the expiration time.
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private TbItemMapper itemMapper;
@Autowired
private TbItemDescMapper itemDescMapper;
@Autowired
private TbItemParamItemMapper itemParamItemMapper;
@Autowired
private RedisDao redisDao;
@Value ("${REDIS_ITEM_KEY}" )
private String REDIS_ITEM_KEY;
@Value ("${ITEM_BASE_KEY}" )
private String ITEM_BASE_KEY;
@Value ("${ITEM_DESC_KEY}" )
private String ITEM_DESC_KEY;
@Value ("${ITEM_PARAM_KEY}" )
private String ITEM_PARAM_KEY;
@Value ("${REDIS_ITEM_EXPIRE}" )
private Integer REDIS_ITEM_EXPIRE;
@Override
public TaotaoResult getItemBaseInfo (long itemId) {
String key = REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_BASE_KEY;
try {
String json = redisDao.get(key);
if (!StringUtils.isBlank(json)) {
TbItem item = JsonUtils.jsonToPojo(json, TbItem.class);
return TaotaoResult.ok(item);
}
} catch (Exception e) {
e.printStackTrace();
}
TbItem item = itemMapper.selectByPrimaryKey(itemId);
try {
redisDao.set(key, JsonUtils.objectToJson(item));
redisDao.expire(key, REDIS_ITEM_EXPIRE);
} catch (Exception e) {
e.printStackTrace();
}
return TaotaoResult.ok(item);
}
@Override
public TaotaoResult getItemDesc (long itemId) {
String key = REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_DESC_KEY;
try {
String json = redisDao.get(key);
if (!StringUtils.isBlank(json)) {
TbItemDesc itemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
return TaotaoResult.ok(itemDesc);
}
} catch (Exception e) {
e.printStackTrace();
}
TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
try {
redisDao.set(key, JsonUtils.objectToJson(itemDesc));
redisDao.expire(key, REDIS_ITEM_EXPIRE);
} catch (Exception e) {
e.printStackTrace();
}
return TaotaoResult.ok(itemDesc);
}
@Override
public TaotaoResult getItemParam (long itemId) {
String key = REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_PARAM_KEY;
try {
String json = redisDao.get(key);
if (!StringUtils.isBlank(json)) {
TbItemParamItem itemParamItem = JsonUtils.jsonToPojo(json, TbItemParamItem.class);
return TaotaoResult.ok(itemParamItem);
}
} catch (Exception e) {
e.printStackTrace();
}
TbItemParamItem itemParamItem = null ;
TbItemParamItemExample example = new TbItemParamItemExample();
Criteria criteria = example.createCriteria();
criteria.andItemIdEqualTo(itemId);
List<TbItemParamItem> list = itemParamItemMapper.selectByExampleWithBLOBs(example);
if (list != null && list.size() > 0 ) {
itemParamItem = list.get(0 );
}
try {
redisDao.set(key, JsonUtils.objectToJson(itemParamItem));
redisDao.expire(key, REDIS_ITEM_EXPIRE);
} catch (Exception e) {
e.printStackTrace();
}
return TaotaoResult.ok(itemParamItem);
}
}
Controller layer
Create ItemController, provide an interface to access basic information, description, and parameters based on id, and return json containing them.
@Controller
@RequestMapping ("/item" )
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping ("/base/{itemId}" )
@ResponseBody
public TaotaoResult getItemBaseInfo (@PathVariable Long itemId) {
TaotaoResult result = itemService.getItemBaseInfo(itemId);
return result;
}
@RequestMapping ("/desc/{itemId}" )
@ResponseBody
public TaotaoResult getItemDesc (@PathVariable Long itemId) {
TaotaoResult result = itemService.getItemDesc(itemId);
return result;
}
@RequestMapping ("/param/{itemId}" )
@ResponseBody
public TaotaoResult getItemParam (@PathVariable Long itemId) {
TaotaoResult result = itemService.getItemParam(itemId);
return result;
}
}
Call service to display product information
need
analyze
After searching for a product, click the link to display the product details page, which initially contains the basic information of the product.
In item.jsp, the basic information is loaded directly by jsp.
Product description and parameters are loaded by ajax.
Commodity parameters are loaded on demand by binding click events.
The product description is loaded with a timer with a delay of 1s.
Summarize
Developed in taotao-portal.
Basic product information:
Request url: /item/{itemId}
Request parameter: product id
Response parameters: product details page view item.jsp, which contains basic product information.
product description:
Request url: /item/desc/{itemId}
Request parameter: product id
Response parameter: product description string.
Basic product information:
Request url: /item/param/{itemId}
Request parameter: product id
Response parameter: product parameter string (including html fragment).
Service layer
The url that needs to be called by the service layer is configured in rest.properties.
ITEM_BASE_URL=/item /base/
ITEM_DESC_URL=/item /desc/
ITEM_PARAM_URL=/item /param /
Create ItemService and the corresponding implementation class, call the rest service according to the product id, and organize the data to return product information, description, and parameters.
@Service
public class ItemServiceImpl implements ItemService {
@Value ("${REST_BASE_URL}" )
private String REST_BASE_URL;
@Value ("${ITEM_BASE_URL}" )
private String ITEM_BASE_URL;
@Value ("${ITEM_DESC_URL}" )
private String ITEM_DESC_URL;
@Value ("${ITEM_PARAM_URL}" )
private String ITEM_PARAM_URL;
@Override
public Item getItemBase (long itemId) {
try {
String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_BASE_URL + itemId);
if (!StringUtils.isBlank(json)) {
TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItem.class);
if (taotaoResult.getStatus() == 200 ) {
TbItem tbItem = (TbItem) taotaoResult.getData();
Item item = new Item();
item.setImage(tbItem.getImage());
item.setId(String.valueOf(tbItem.getId()));
item.setPrice(tbItem.getPrice());
item.setSellPoint(tbItem.getSellPoint());
item.setTitle(tbItem.getTitle());
return item;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
@Override
public String getItemDesc (Long itemId) {
try {
String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_DESC_URL + itemId);
if (!StringUtils.isBlank(json)) {
TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItemDesc.class);
if (taotaoResult.getStatus() == 200 ) {
TbItemDesc itemDesc = (TbItemDesc) taotaoResult.getData();
String desc = itemDesc.getItemDesc();
return desc;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
@Override
public String getItemParam (Long itemId) {
try {
String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_PARAM_URL + itemId);
if (!StringUtils.isBlank(json)) {
TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItemParamItem.class);
if (taotaoResult.getStatus() == 200 ) {
TbItemParamItem itemParamItem = (TbItemParamItem) taotaoResult.getData();
String paramData = itemParamItem.getParamData();
List<Map> jsonList = JsonUtils.jsonToList(paramData, Map.class);
StringBuffer sb = new StringBuffer();
sb.append(
"<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">\n" );
sb.append(" <tbody>\n" );
for (Map m1 : jsonList) {
sb.append(" <tr>\n" );
sb.append(" <th class=\"tdTitle\" colspan=\"2\">" + m1.get("group" ) + "</th>\n" );
sb.append(" </tr>\n" );
List<Map> list2 = (List<Map>) m1.get("params" );
for (Map m2 : list2) {
sb.append(" <tr>\n" );
sb.append(" <td class=\"tdTitle\">" + m2.get("k" ) + "</td>\n" );
sb.append(" <td>" + m2.get("v" ) + "</td>\n" );
sb.append(" </tr>\n" );
}
}
sb.append(" </tbody>\n" );
sb.append("</table>" );
return sb.toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "" ;
}
}
Controller layer
Create an ItemController, assign an object with basic information about the item in the item view, and provide a query interface for description and parameters.
@Controller
@RequestMapping ("/item" )
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping ("/{itemId}" )
public String showItem (@PathVariable Long itemId, Model model) {
Item item = itemService.getItemBase(itemId);
model.addAttribute("item" , item);
return "item" ;
}
@RequestMapping (value = "/desc/{itemId}" , produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8" )
@ResponseBody
public String getItemDesc (@PathVariable Long itemId) {
String string = itemService.getItemDesc(itemId);
return string;
}
@RequestMapping (value = "/param/{itemId}" , produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8" )
@ResponseBody
public String getItemParam (@PathVariable Long itemId) {
String string = itemService.getItemParam(itemId);
return string;
}
}
Run the project
Start portal, rest, search, solr, nginx, and click the product link from the search result.
Basic product information.
product description.
Product parameters.