サーバーとフロントはJSON通信を採用
3489 ワード
エンティティークラスのカプセル化(サーバから返される情報はすべてエンティティーに格納されます)
サーバはjson文字列を返します
フロント解析json文字列(fastjsonを使用)手順
参考記事:FastJSONを使用して、オブジェクトまたは配列とJSON列を相互に回転
http://blog.csdn.net/gaojinshan/article/details/30260707
package com.chinaseacom.store.common;
public class ResponseBody {
public static final int CODE_SUCCESS=1;
public static final int CODE_FAIL=0;
private int code=1; //0:failtrue; 1:success
private String message; //
private String errors;
private String accessToken;
private Object result;
public ResponseBody(int code, String message, String errors, Object result, String accessToken) {
super();
this.code = code;
this.message = message;
this.errors = errors;
this.result = result;
this.accessToken = accessToken;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
/**
* @return the code
*/
public int getCode() {
return code;
}
/**
* @param code the code to set
*/
public void setCode(int code) {
this.code = code;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
/**
* @return the errors
*/
public String getErrors() {
return errors;
}
/**
* @param errors the errors to set
*/
public void setErrors(String errors) {
this.errors = errors;
}
/**
* @return the result
*/
public Object getResult() {
return result;
}
/**
* @param result the result to set
*/
public void setResult(Object result) {
this.result = result;
}
}
サーバはjson文字列を返します
protected void writeResponseByJsonStr(Object javabean,int code, String message, String error){
try {
HttpServletResponse response= getResponse();
response.setContentType("application/json; charset=utf-8");
response.setHeader("Cache-Control", "no-cache"); //
PrintWriter out = response.getWriter();
out.print(JSON.toJSONString(new ResponseBody(code,message,error,javabean,accessToken) ));// json
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
フロント解析json文字列(fastjsonを使用)手順
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinaseacom.store.customer.model.Notice;
public class MobileClient {
public static void main(String[] args) throws IOException {
BufferedReader reader = null;
try {
URL url = new URL("http://localhost:8080/customer/notice/list");
reader = new BufferedReader(new InputStreamReader(url.openStream()));
String jsonString = reader.readLine();
System.out.println(jsonString);
JSONObject jsonObject = JSON.parseObject(jsonString); // json
JSONArray jsonArray = (JSONArray) jsonObject.get("result");// key json
System.out.println(jsonArray.toJSONString());
List<Notice> nlist = JSON.parseArray(jsonArray.toJSONString(),
Notice.class); // json
for (Notice notice : nlist) {
System.out.println(notice.getUpdateDate());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
reader.close();
}
}
}
参考記事:FastJSONを使用して、オブジェクトまたは配列とJSON列を相互に回転
http://blog.csdn.net/gaojinshan/article/details/30260707