远程接口调用

1.传输格式.

传输格式均为JSON字符串,使用spring mvc返回对象,并自动通过fastjson,jackson工具类(JsonMapper.java)进行对象到JSON转换。

输出格式符合JSON标准,UTF-8编码

2.在服务器端.

  1. 通过@RequestMapping注解定义一个函数,定义要拦截的url
  2. 一般接口返回不是页面,而是Object,List的值对象,
  3. 要在函数上面加上@ResponseBody,要把值对象转换成JSon对象
1
2
3
4
5
6
7
8
@RequestMapping (value = "getCountByStuCode/{stuCode}" )
@ResponseBody
public List getCountByStuCode( @PathVariable String stuCode) {
     EtDiscipline entity = new EtDiscipline();
     entity.setStuCode(stuCode);
     List<DicMessage> list = etDisciplineService.count(entity);
     return list ;
}

3.在客户端

  1. 首先要准备JDK自带的HttpUrlConnection或者Apacle的HttpClient来实现JAVA的Http协议的访问,下面例子是通过HttpUrlConnection封装了一个提交数据的工具类.这个类基本的看懂就可以,不必要细究,原理是通过http协议模拟浏览器向远程发送数据

    package com.thinkgem.jeesite.common.utils;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    /**
      * HTTP工具类
      *
      */
    public class PostServer {
         
         /**
          * @param param
          * @param url
          * @return
          */
         public static String SendPost(String param, String url) {
             OutputStreamWriter out = null ;
             BufferedReader in = null ;
             String result = "" ;
             try {
                 URL realUrl = new URL(url);
                 HttpURLConnection conn = (HttpURLConnection) realUrl
                         .openConnection();
                 conn.setDoOutput( true );
                 conn.setDoInput( true );
                 conn.setUseCaches( false );
                 conn.setRequestMethod( "POST" );
                 conn.setConnectTimeout( 50000 );
                 conn.setReadTimeout( 50000 );
                 conn.setRequestProperty( "Content-Type" , "application/json" );
                 conn.setRequestProperty( "Accept" , "application/json" );
                 conn.setRequestProperty( "tag" , "htc_new" );
                 conn.connect();
                 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8" );
                 out.write(param);
                 out.flush();
                 out.close();
                 //
                 in = new BufferedReader( new InputStreamReader(
                         conn.getInputStream(), "UTF-8" ));
                 String line = "" ;
                 while ((line = in.readLine()) != null ) {
                     result += line;
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 try {
                     if (out != null ) {
                         out.close();
                     }
                     if (in != null ) {
                         in.close();
                     }
                 } catch (IOException ex) {
                     ex.printStackTrace();
                 }
             }
             return result;
         }
    }
  2. 通过上述类先对系统进行模拟登录,提交账号和密码,得到登录后的sessionId

    private static String getLoginSessionId(String userName, String password) {
         // 封装请求参数
         JSONObject json = new JSONObject();
         json.put( "isValidateCodeLogin" , "false" );
         String result = PostServer.SendPost(json.toString(), "<a href="http://127.0.0.1:8888/jeesite/a/login?__ajax=true&mobileLogin=true&isValidateCodeLogin=false&username=" "="" style="color: rgb(59, 115, 175); text-decoration: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-size: 14px !important; min-height: auto !important;">http://127.0.0.1:8888/jeesite/a/login?__ajax=true&mobileLogin=true&isValidateCodeLogin=false&username=" + userName + "&password=admin" );
         json = JSON.parseObject(result);
         result = json.get( "sessionid" ).toString();
         return result;
    }
  3. 得到sessionId后,然后在每次请求里面都加上session的后缀,这样就类似于当前用户登录去访问这个请求了,具体格式如下

    在请求路径后包含会话ID(JSESSIONID一定要大写)即可,格式如下:

    URL  = “请求URL” + “;JSESSIONID=”+ “会话ID”

    http://127.0.0.1:8080/app/a/test/test/listData;JSESSIONID=b6b486a8919e4fc196358e10b6a82a2b?__ajax=true

    通过一个工具类调用远程API的代码

    public String getScopeBySystem(String stuNumber) {
         JSONObject json = new JSONObject();
         String result = PostServer.SendPost(json.toString(),
                 "<a href="http://127.0.0.1:8888/jeesite/a/getCountByStuCode/" "="" style="color: rgb(59, 115, 175); text-decoration: none; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-size: 14px !important; min-height: auto !important;">http://127.0.0.1:8888/jeesite/a/getCountByStuCode/" + stuNumber + ";JSESSIONID="
                         + sessionId + "" );
         return result;
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值