欢迎您来到懒之才-站长的分享平台!   学会偷懒,并懒出境界是提高工作效率最有效的方法!
首页 > 经验分享 > 其他经验 > 跨域访问与远程调用的简洁之道

跨域访问与远程调用的简洁之道

2018-09-21 708 收藏 0 赞一个 0 真差劲 0 去评论

前端不想和后端绑定啦,它想蹭百家!系统A有意系统B,它想来一次亲密接触! 于是乎,跨域访问火了!远程调用火了!那么,最简单使用的跨域访问和远程调又是什么呢?请看下面道来。 基于cors的跨域访问:    

上代码:

    @RequestMapping(method = RequestMethod.DELETE)
    @CrossOrigin(origins = {"http://localhost:10003"}, methods = RequestMethod.DELETE)
    public ResponseResult deletePartnerProfile(String ids) {
 
        ResponseResult responseResult = super.buildResponseResult(ids);
        if(ids == null || !ids.matches("[\\d+,\\d*]+,")) {
            return  responseResult.result(HttpStatus.BAD_REQUEST);
        }
        //执行删除操作
        getPartnerProfileService().deletePartnerProfile(ids);
        return responseResult.result(HttpStatus.OK, "success");
    }

找关键的时间到了!看仔细,看仔细,是的呢,如果你注意到了@CrossOrigin,那么恭喜你,你已经会跨域访问了。     

 @CrossOrigin是springMVC中提供的用于实现跨域访问的注解,它等同用拦截器在响应头中添加Access-Control-Allow-Origin: #{URL}。客服端想服务器发送请求时,第一次请求用于获取权限,当服务器返回的响应中含有上述内容时,则再次发出请求,取得资源。 基于RestTempalte的远程调用    

上代码:

(先分享一下自己写的工具类。。。。)

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.group.entity.vo.VoPageContent;
import com.pparking.base.commons.domain.PageCondition;
import com.pparking.core.commons.domain.ResponseResult;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
 
public class RestTemplateUtils {
 
    //获取restTemplate对象,并设置字符串为UTF-8
    public static RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
 
        return restTemplate;
    }
 
 
    //get方式获取远程资源
    public static String getRestObject(String url,Object params) {
        Map<String, Object> mapParam = null;
        if(params instanceof Map) {
            mapParam = (Map<String, Object>) params;
        }else{
 
            mapParam = BeanUtils.beanToMap(params);//将参数转化为map对象(只能接受基本数据类型)
        }
        return restTemplate().getForObject(url,String.class,mapParam);
    }
 
    //post方式获取远程资源
    public static String postRestObject(String url,MultiValueMap<String, Object> params) {
 
        return restTemplate().postForObject(url,params,String.class);
    }
    //解析远程资源为页面内容对象
    public static VoPageContent getVoPageContent(String restResult,Class clazz) {
        ResponseResult result = JSONObject.parseObject(restResult, ResponseResult.class);
 
        Map<String,Object> map = (Map<String, Object>) result.getBody();
        List list = null;
        Assert.notNull(map,"停车场验证错误");
        if(map.get("rows") != null) {
             list = JSONArray.parseArray(map.get("rows").toString(), clazz);
        }
        Long total = null;
        if(map.get("total") !=null) {
            total = JSON.parseObject(map.get("total").toString(), Long.class);
        }
        PageCondition pageCondition = null;
        if(map.get("condition") !=null) {
            pageCondition = JSONArray.parseObject(map.get("condition").toString(), PageCondition.class);
        }
 
        Long totalMoney = null;
        if(map.get("totalMoney") !=null) {
            totalMoney = JSONArray.parseObject(map.get("totalMoney").toString(), Long.class);
        }
        return new VoPageContent<>(total,pageCondition,list,totalMoney) ;
    }
    }

借用我的工具类后,一切so 简单。。。

 public void logout(Long userid) {
        String username = getGroup(userid).getUsername();
        cacheManager.getCache("groupCache").evict(username);
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("userid", userid);
        RestTemplateUtils.postRestObject("http://localhost:10000/group/logout", map);
    }

最后,郑重声明,我给大家的只是一个引,真正具体的使用,需要根据实际需求进行选择性详细学习

一、推荐使用迅雷或快车等多线程下载软件下载本站资源。

二、未登录会员无法下载,登录后可获得更多便利功能,若未注册,请先注册。

三、如果服务器暂不能下载请稍后重试!总是不能下载,请点我报错 ,谢谢合作!

四、本站大部分资源是网上搜集或私下交流学习之用,任何涉及商业盈利目的均不得使用,否则产生的一切后果将由您自己承担!本站将不对任何资源负法律责任.如果您发现本站有部分资源侵害了您的权益,请速与我们联系,我们将尽快处理.

五、如有其他问题,请加网站设计交流群(点击这里查看交流群 )进行交流。

六、如需转载本站资源,请注明转载来自并附带链接

七、本站部分资源为加密压缩文件,统一解压密码为:www.aizhanzhe.com

大家评论