1.传输格式.
传输格式均为JSON字符串,使用spring mvc返回对象,并自动通过fastjson,jackson工具类(JsonMapper.java)进行对象到JSON转换。
输出格式符合JSON标准,UTF-8编码
2.在服务器端.
- 通过@RequestMapping注解定义一个函数,定义要拦截的url
- 一般接口返回不是页面,而是Object,List的值对象,
- 要在函数上面加上@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.在客户端
-
首先要准备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;
}
}
-
通过上述类先对系统进行模拟登录,提交账号和密码,得到登录后的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;
}
- 得到sessionId后,然后在每次请求里面都加上session的后缀,这样就类似于当前用户登录去访问这个请求了,具体格式如下
在请求路径后包含会话ID(JSESSIONID一定要大写)即可,格式如下:
URL = “请求URL” + “;JSESSIONID=”+ “会话ID”
通过一个工具类调用远程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;
}