1.
package oapi.com.service;
import oapi.com.IService;
public class TestService implements IService{
public String execute(String data) {
String test = "这是一个测试的数据";
return test;
}
}
2.
package oapi.com.util;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class HttpUtil {
public static void writeResponse(HttpServletResponse response, String str) {
if(str==null || str.length()==0)
return;
if(str.length() < 300)
//log.info("writeResponse:" + str);
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter writer = null;
try {
response.setContentLength(str.getBytes("utf-8").length);
writer = response.getWriter();
writer.print(str);
writer.flush();
} catch (Exception e) {
} finally {
if(writer != null) {
writer.close();
}
}
}
}
3.
package oapi.com.util;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class StringUtil {
public static boolean isEmpty(String str)
{
return str == null || str.length() == 0;
}
public static boolean isNotEmpty(String str)
{
return !isEmpty(str);
}
}
4.
package oapi.com;
import org.json.JSONObject;
public interface IService {
public String execute(String data);
}
5.
package oapi.com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import oapi.com.util.HttpUtil;
import oapi.com.util.StringUtil;
public class LoginServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 4028644947121632639L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
// 校验参数是否为空
if(StringUtil.isEmpty(name) || StringUtil.isEmpty(pwd)){
HttpUtil.writeResponse(response, getResponseJson(1001, "缺少参数", ""));
return ;
}
// 如果用户名不等于lisi或者密码不等于123456,则校验失败
if(!name.equals("fish") || !pwd.equals("fish")){
HttpUtil.writeResponse(response, getResponseJson(1002, "用户名或密码错误", ""));
return ;
}
// 验证通过
HttpUtil.writeResponse(response, getResponseJson(1000, "登录成功", ""));
}
private String getResponseJson(int return_code,String return_msg,String data){
if(StringUtil.isEmpty(data)){
return "{\"return_code\":"+return_code+",\"return_msg\":\""+return_msg+"\"}";
}else{
return "{\"return_code\":"+return_code+",\"return_msg\":\""+return_msg+"\",\"data\":"+data+"}";
}
}
}
6.
package oapi.com;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;
import oapi.com.util.HttpUtil;
import oapi.com.util.StringUtil;
public class MainServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 4558261663730128486L;
public static Map<String,IService> serviceMaps = new HashMap<String, IService>();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method"); //API业务处理接口名称
if(StringUtil.isEmpty(method)){
HttpUtil.writeResponse(response, getResponseJson(1003, "缺少参数", ""));
}else{
String className = getServiceClassName(method);
IService service = getServiceInstance(className);
if(service == null){
HttpUtil.writeResponse(response, getResponseJson(1006, "未找到接口处理类", ""));
return ;
}
try {
String data = service.execute("");
HttpUtil.writeResponse(response, getResponseJson(0, "请求成功", data));
} catch (Exception e) {
HttpUtil.writeResponse(response, getResponseJson(1005, "参数格式有误", ""));
}
}
}
private String getServiceClassName(String method){
method = method.substring(0,1).toUpperCase()+method.substring(1); //第一个字母转大写
Pattern pattern = Pattern.compile("\\.([a-z]{1})"); //点后面的第一字母转大写并去掉小数点
Matcher matcher = pattern.matcher(method);
while(matcher.find()){
method = method.replace(matcher.group(), matcher.group(1).toUpperCase());
}
return "oapi.com.service."+method+"Service";
}
private IService getServiceInstance(String className){
IService service = serviceMaps.get(className);
if(service == null){
try {
service = (IService)(Class.forName(className).newInstance());
serviceMaps.put(className, service);
} catch (Exception e) {
//log.error("开放接口-请求接口的方法名不正确,serviceClassName=" + className);
}
}
return service;
}
private String getResponseJson(int return_code,String return_msg,String data){
if(StringUtil.isEmpty(data)){
return "{\"return_code\":"+return_code+",\"return_msg\":\""+return_msg+"\"}";
}else{
return "{\"return_code\":"+return_code+",\"return_msg\":\""+return_msg+"\",\"data\":"+data+"}";
}
}
}
7
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>oapi</display-name>
<servlet>
<servlet-name>OpenApiServlet</servlet-name>
<servlet-class>oapi.com.MainServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>OpenApi2Servlet</servlet-name>
<servlet-class>oapi.com.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>OpenApi3Servlet</servlet-name>
<servlet-class>oapi.com.TicketServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OpenApiServlet</servlet-name>
<url-pattern>/openapi</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>OpenApi2Servlet</servlet-name>
<url-pattern>/openapi2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>OpenApi3Servlet</servlet-name>
<url-pattern>/openapi3</url-pattern>
</servlet-mapping>
</web-app>