支持jsonP的Controller写法
package com.taotao.sso.controller;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.converter.json.MappingJacksonValue;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.taotao.common.pojo.TaotaoResult;import com.taotao.common.utils.ExceptionUtil;import com.taotao.sso.service.UserService;@Controller@RequestMapping("/user")public class UserController { ???@Autowired ???private UserService userService; ???/**校验用户名、电话、邮箱是否重复方法* 接口文档:* 请求方法 ???GETURL ???http://sso.taotao.com/user/check/{param}/{type}参数说明 ???格式如:zhangsan/ 1,其中zhangsan是校验的数据,type为类型,可选参数1、2、3分别代表username、phone、email可选参数callback:如果有此参数表示此方法为jsonp请求,需要支持jsonp。*/ ???@RequestMapping("/check/{param}/{type}") ???@ResponseBody ???public Object checkData(@PathVariable String param,@PathVariable Integer type,String callback){ ???????//返回结果 ???????TaotaoResult result = null; ???????//校验参数是否正确(注意:在Controller中校验即可,service中可以不校验了) ???????if (StringUtils.isEmpty(param)) { ???????????result = TaotaoResult.build(400, "校验内容不能为空"); ???????} ???????if (type==null) { ???????????result = TaotaoResult.build(400, "校验内容参数不能为空"); ???????} ???????if (1!=type && 2!=type && 3!=type) { ???????????result = TaotaoResult.build(400, "校验内容类型错误"); ???????} ???????//说明参数异常需要提前返回 ???????if (result!=null) { ???????????//判断是否需要支持jsonP ???????????if (callback!=null) { ???????????????//需要将返回结果封装成支持jsonP的形式(注意:这种返回json支持的写法) ???????????????MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result); ???????????????mappingJacksonValue.setJsonpFunction(callback); ???????????????return mappingJacksonValue; ???????????}else{ ???????????????return result; ???????????} ???????} ???????//因为是提供接口服务,所以要处理可能出现的逻辑上的异常 ???????try { ???????????//调用service执行正常的业务逻辑 ???????????result = userService.checkData(param, type); ???????} catch (Exception e) { ???????????result = TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); ???????} ???????//正常返回也需要判断是否需要jsonP ???????if (null!=callback) { ???????????MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result); ???????????mappingJacksonValue.setJsonpFunction(callback); ???????????return mappingJacksonValue; ???????}else{ ???????????return result; ???????} ???}}
附 service(仅供参考异常处理位置):
package com.taotao.sso.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.taotao.common.pojo.TaotaoResult;import com.taotao.mapper.TbUserMapper;import com.taotao.pojo.TbUser;import com.taotao.pojo.TbUserExample;import com.taotao.pojo.TbUserExample.Criteria;import com.taotao.sso.service.UserService;/** * 用户管理的service * @author Administrator */@Servicepublic class UserServiceImpl implements UserService { ???????@Autowired ???private TbUserMapper userMapper; ???//校验用户名、电话、邮箱 是否不重复 ???@Override ???public TaotaoResult checkData(String content, Integer type) { ???????//创建查询对象 ???????TbUserExample example = new TbUserExample(); ???????Criteria criteria = example.createCriteria(); ???????//封装查询条件 ???????switch (type) { ???????case 1: ???????????criteria.andUsernameEqualTo(content); ???????????break; ???????case 2: ???????????criteria.andPhoneEqualTo(content); ???????????break; ???????case 3: ???????????criteria.andEmailEqualTo(content); ???????????break; ???????} ???????//因为在Controller层中调用此接口前就已经校验过 type的值一定为123中的一个,所以这里不用再次校验了 ???????//执行查询 ???????List<TbUser> list = userMapper.selectByExample(example); ???????if (list!=null && list.size()>0) { ???????????return TaotaoResult.ok(false); ???????} ???????return TaotaoResult.ok(true); ???}}
支持jsonP的Controller写法
原文地址:https://www.cnblogs.com/libin6505/p/9831574.html