大佬教程收集整理的这篇文章主要介绍了支持jsonP的Controller写法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
支持jsonP的Controller写法
@H_874_6@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; /**校验用户名、电话、邮箱是否重复方法 * 接口文档: * 请求方法 GET URL 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; } } }
@H_874_6@
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 */ @service public 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写法全部内容,希望文章能够帮你解决支持jsonP的Controller写法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。