• 跨域访问接口,传递参数


        //接收方式一( jeesite ssm)
        @RequestMapping(value = "csDemo")
        public @ResponseBody String csDemo(HttpServletRequest request,HttpServletResponse response,Model model) throws IOException{
            SysChargeRegister sysChargeRegister = JSON.parseObject(request.getInputStream(),SysChargeRegister.class);
            System.out.println("sysChargeRegister.getProjectName-------------"+sysChargeRegister.getProjectName());
            System.out.println("sysChargeRegistergetProjectNumbers.-------------"+sysChargeRegister.getProjectNumbers());
            return "cds";
        }
        spring-context-shiro.xml ${adminPath}/csController/csController/csDemo = anon //shiro权限
        spring-mvc.xml <mvc:exclude-mapping path="${adminPath}/csController/csController/csDemo"/>//拦截设置
        
        //接收方式二(springboot)
        @ResponseBody
        @RequestMapping(value={"/csDemo"})
        public String csDemo(@RequestBody SysProject sysProject) throws Exception{
            String result = "";
    
            String projectName = sysProject.getProjectName();
            String projectNumbers = sysProject.getProjectNumbers();
    
            System.out.println("projectName-----" + projectName);
            System.out.println("projectNumbers-----" + projectNumbers);
            return result;
        }
        在对应的shiro设置文件中,添加对应的权限 开放csDemo接口  “anon”
        
        //发送方
        public static void main(String[] args) throws MalformedURLException {
            String url2="http://127.0.0.1:8080/csController/csController/csDemo";
            JSONObject jsonObject2=new JSONObject();
            jsonObject2.put("projectName","项目名=-=-=-==-=");
            jsonObject2.put("projectNumbers","项目编码=======-=-=-=-");
            doPost2(url2,jsonObject2);
        }
        public static String doPost2(String url, JSONObject param) {
            HttpPost httpPost = null;
            String result = null;
            try {
                HttpClient client = new DefaultHttpClient();
                httpPost = new HttpPost(url);
                if (param != null) {
                    StringEntity se = new StringEntity(param.toString(), "utf-8");
                    httpPost.setEntity(se); // post方法中,加入json数据
                    httpPost.setHeader("Content-Type", "application/json");
                }
    
                HttpResponse response = client.execute(httpPost);
                if (response != null) {
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                        result = EntityUtils.toString(resEntity, "utf-8");
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return result;
        }
  • 相关阅读:
    剑指offer:2.二维数组的查找(Java版)
    剑指offer:1.找出数组中重复的数(java版)
    Java自动内存管理机制学习(二):垃圾回收器与内存分配策略
    Java自动内存管理机制学习(一):Java内存区域与内存溢出异常
    Java并发编程学习:线程安全与锁优化
    Java并发编程学习:volatile关键字解析
    Java 8之重新认识HashMap
    【转】java内部类的作用分析
    不能进入String.class调试
    SCJP考试题310-025(第二套<4>)92-147/147
  • 原文地址:https://www.cnblogs.com/ljc1212/p/15261345.html
Copyright © 2020-2023  润新知