淘淘商城第五天笔记(15)

发布时间:2021-06-09

传智播客Java学院传智.入云龙

} else {

//如果是叶子节点

String item = "/products/"+tbItemCat.getId()+".html|" + tbItemCat.getName();

resultList.add(item);

}

}

return resultList;

}

}

5.4 Controller

响应一个json数据。判断callback参数是否为空,如果为空正常返回json数据,如果不为空,支持jsonp调用。

需要使用@ResponseBody注解。

支持jsonp两种方式:

第一种:直接响应字符串

@Controller

@RequestMapping("/item/cat")

publicclass ItemCatController {

@Autowired

private ItemCatService itemCatService;

@RequestMapping(value="/list",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")

@ResponseBody

public String getItemCatList(String callback) {

ItemCatResult result = itemCatService.getItemCatList();

if (StringUtils.isBlank(callback)) {

//需要把result转换成字符串

String json = JsonUtils.objectToJson(result);

return json;

}

//如果字符串不为空,需要支持jsonp调用

//需要把result转换成字符串

String json = JsonUtils.objectToJson(result);

return callback + "(" + json + ");";

}

}

第二种方法:

精彩图片

热门精选

大家正在看