make all response return as json format

This commit is contained in:
Sola
2015-11-07 01:09:13 +08:00
parent fc9474e576
commit eeae874ded
6 changed files with 146 additions and 9 deletions

View File

@@ -1,6 +1,8 @@
package love.sola.netsupport.api;
import com.google.gson.Gson;
import love.sola.netsupport.enums.ResponseCode;
import love.sola.netsupport.pojo.User;
import love.sola.netsupport.sql.SQLQuery;
import javax.servlet.ServletConfig;
@@ -21,9 +23,12 @@ import java.io.PrintWriter;
@WebServlet(name = "GetUser",urlPatterns = "/api/getuser",loadOnStartup = 1)
public class GetUser extends HttpServlet {
private Gson gson = null;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
gson = new Gson();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
@@ -33,15 +38,28 @@ public class GetUser extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.addHeader("Content-type", "text/json;charset=utf-8");
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
String name = request.getParameter("name");
if (name == null) {
response.addHeader("Content-type", "text/plain;charset=utf-8");
out.println("Username required.");
if ((id == null || id.isEmpty()) && (name == null || name.isEmpty())) {
out.println(gson.toJson(new Response(ResponseCode.PARAMETER_REQUIRED)));
} else if (id != null) {
try {
User u = SQLQuery.getUserById(Integer.parseInt(id));
if (u == null)
out.println(gson.toJson(new Response(ResponseCode.USER_NOT_FOUND)));
else
out.println(gson.toJson(new Response(ResponseCode.OK, u)));
} catch (NumberFormatException e) {
out.println(gson.toJson(new Response(ResponseCode.ILLEGAL_PARAMETER)));
}
} else {
response.addHeader("Content-type", "text/json;charset=utf-8");
Gson gson = new Gson();
out.println(gson.toJson(SQLQuery.getUserFromName(name)));
User u = SQLQuery.getUserByName(name);
if (u == null)
out.println(gson.toJson(new Response(ResponseCode.USER_NOT_FOUND)));
else
out.println(gson.toJson(new Response(ResponseCode.OK, u)));
}
out.close();
}

View File

@@ -0,0 +1,29 @@
package love.sola.netsupport.api;
import lombok.AllArgsConstructor;
import love.sola.netsupport.enums.ResponseCode;
/**
* ***********************************************
* Created by Sola on 2015/11/5.
* Don't modify this source without my agreement
* ***********************************************
*/
@AllArgsConstructor
public class Response {
public int code;
public String info;
public Object result;
public Response(ResponseCode code) {
this(code, null);
}
public Response(ResponseCode code, Object result) {
this.code = code.id;
this.info = code.info;
this.result = result;
}
}

View File

@@ -11,7 +11,6 @@ import java.util.Map;
*/
public enum ISPType {
TELECOM("Telecom", 1),
UNICOM("Unicom", 2),
CHINAMOBILE("ChinaMobile", 3),;
@@ -53,4 +52,5 @@ public enum ISPType {
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,47 @@
package love.sola.netsupport.enums;
import java.util.HashMap;
import java.util.Map;
/**
* ***********************************************
* Created by Sola on 2015/11/6.
* Don't modify this source without my agreement
* ***********************************************
*/
public enum ResponseCode {
OK(0, "OK"),
PARAMETER_REQUIRED(-1, "Parameter Required"),
ILLEGAL_PARAMETER(-2, "Illegal parameter"),
USER_NOT_FOUND(-11, "User not found"),
;
private static final Map<Integer, ResponseCode> ID_MAP = new HashMap<>();
static {
for (ResponseCode type : values()) {
if (type.id > 0) {
ID_MAP.put(type.id, type);
}
}
}
public final String info;
public final int id;
ResponseCode(int id, String info) {
this.info = info;
this.id = id;
}
public static ResponseCode fromId(int id) {
return ID_MAP.get(id);
}
@Override
public String toString() {
return info;
}
}

View File

@@ -37,7 +37,11 @@ public class SQLQuery {
}
}
public static User getUserFromName(String name) {
public static void free(Connection conn) {
if (conn != null) try { conn.close(); } catch (SQLException e) { }
}
public static User getUserByName(String name) {
Connection conn = null;
try {
conn = ds.getConnection();
@@ -54,10 +58,31 @@ public class SQLQuery {
}
} catch (SQLException e) {
} finally {
if (conn != null) try { conn.close(); } catch (SQLException e) { }
free(conn);
}
return null;
}
public static User getUserById(int id) {
Connection conn = null;
try {
conn = ds.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT * FROM user_info WHERE id=?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return new User(rs.getInt(COLUMN_ID),
rs.getString(COLUMN_NAME),
rs.getLong(COLUMN_STUDENT_ID),
rs.getString(COLUMN_NET_ACCOUNT),
ISPType.fromId(rs.getInt(COLUMN_ISP)),
rs.getString(COLUMN_WECHAT));
}
} catch (SQLException e) {
} finally {
free(conn);
}
return null;
}
}

View File

@@ -1,5 +1,11 @@
package love.sola.netsupport.wechat.handler;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import java.util.HashMap;
import java.util.Map;
/**
* ***********************************************
* Created by Sola on 2015/11/5.
@@ -7,4 +13,16 @@ package love.sola.netsupport.wechat.handler;
* ***********************************************
*/
public class HandlerList {
public static Map<String, Class<? extends WxMpMessageHandler>> handlers = new HashMap<>();
static {
handlers.put("Register", RegisterHandler.class);
}
public void init(WxMpMessageRouter router) {
}
}