跨域访问接口,传递参数

复制代码
    //接收方式一( 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;
    }
复制代码

 

posted @   景、  阅读(307)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示