java springmvc的普通接口和跨域接口
现在的bs架构和移动端架构,都需要进行接口的对接。
android和ios访问接口时,并没有跨域的限制。web端的访问接口时具有跨域的限制。
为了实现前端与后端的混合搭配模式(前端和后端多对多通信模式)
java后台需要根据业务需要提供两种接口以完全满足前后端的混合搭配模式。
1、普通接口,提供给所有的android、ios、以及不跨域的web访问
@RequestMapping("versionVerify")
@ResponseBody
public AjaxJson versionVerify(String internetBarId, String versionCode, HttpServletRequest req)
{
Map<String,Object> map = new HashMap();
AjaxJson j = new AjaxJson();
j.setMsg("Ok");
j.setAttributes(map);
HashMap<String,Object> map2 = new HashMap<String,Object>();
map2.put("internetBarId", internetBarId);
map2.put("versionCode", versionCode);
j.setObj(map2);
j.setSuccess(true);
if ( (false == StringUtil.isNotEmpty(internetBarId)) || (false == StringUtil.isNotEmpty(versionCode)) )
{
j.setMsg("null input");
j.setAttributes(null);
j.setSuccess(false);
j.setObj(null);
return j;
}
return j;
}
2. 基于jsonp标准实现的跨域接口,给跨域的web端访问
@RequestMapping("testjsonp")
@ResponseBody
public JSONPObject testjsonp(String callback, HttpServletRequest req)
{
Map<String, String> map = new HashMap<String, String>();
map.put("aaa", "I'm kerry!");
return new JSONPObject(callback, map); //hellojsonp({"aaa":"I'm kerry!"})
}