master
parent
4fb07bbb56
commit
15b0a7ed7c
2
pom.xml
2
pom.xml
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
<groupId>cn.cloudowr</groupId>
|
<groupId>cn.cloudowr</groupId>
|
||||||
<artifactId>sdk</artifactId>
|
<artifactId>sdk</artifactId>
|
||||||
<version>1.1.2</version>
|
<version>1.2.3</version>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
package cn.cloudowr.sdk.jfinal;
|
||||||
|
|
||||||
|
import cn.cloudowr.sdk.FastJSONUtil;
|
||||||
|
import com.jfinal.core.ActionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyf66 on 2017/2/20.
|
||||||
|
*/
|
||||||
|
public class Controller extends com.jfinal.core.Controller{
|
||||||
|
@Override
|
||||||
|
public void renderError(int errorCode, String msg) {
|
||||||
|
throw new ActionException(errorCode, new ErrorRender(errorCode, msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void renderJson(Object o) {
|
||||||
|
renderJson(FastJSONUtil.bsonToJSON(o));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
package cn.cloudowr.sdk.jfinal;
|
||||||
|
|
||||||
|
import com.jfinal.aop.Interceptor;
|
||||||
|
import com.jfinal.aop.Invocation;
|
||||||
|
import com.jfinal.core.ActionException;
|
||||||
|
import com.jfinal.render.Render;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyf66 on 2017/2/20.
|
||||||
|
*/
|
||||||
|
public class ErrorInterceptor implements Interceptor {
|
||||||
|
@Override
|
||||||
|
public void intercept(Invocation inv) {
|
||||||
|
try {
|
||||||
|
inv.invoke();
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (e instanceof ActionException) {
|
||||||
|
ActionException ae = (ActionException) e;
|
||||||
|
Render render = ae.getErrorRender();
|
||||||
|
render.render();
|
||||||
|
} else {
|
||||||
|
new ErrorRender(500, inv.getController().getResponse()).render();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
package cn.cloudowr.sdk.jfinal;
|
||||||
|
|
||||||
|
import com.jfinal.render.Render;
|
||||||
|
import com.jfinal.render.RenderException;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyf66 on 2017/2/20.
|
||||||
|
*/
|
||||||
|
public class ErrorRender extends Render {
|
||||||
|
protected static final String contentType = "text/html; charset=" + getEncoding();
|
||||||
|
|
||||||
|
private int errorCode;
|
||||||
|
private String msg;
|
||||||
|
|
||||||
|
public ErrorRender(int errorCode) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.msg = errorCode + "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorRender(int errorCode, String msg) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ErrorRender(int errorCode, HttpServletResponse response) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.response = response;
|
||||||
|
this.msg = errorCode + "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render() {
|
||||||
|
response.setStatus(errorCode);
|
||||||
|
|
||||||
|
PrintWriter writer = null;
|
||||||
|
try {
|
||||||
|
response.setContentType(contentType);
|
||||||
|
writer = response.getWriter();
|
||||||
|
writer.write(msg);
|
||||||
|
writer.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RenderException(e);
|
||||||
|
} finally {
|
||||||
|
if (writer != null)
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getErrorCode() {
|
||||||
|
return errorCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorCode(int errorCode) {
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMsg() {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMsg(String msg) {
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package cn.cloudowr.sdk;
|
package cn.cloudowr.sdk.jfinal;
|
||||||
|
|
||||||
import com.jfinal.aop.Interceptor;
|
import com.jfinal.aop.Interceptor;
|
||||||
import com.jfinal.aop.Invocation;
|
import com.jfinal.aop.Invocation;
|
||||||
|
|
@ -14,7 +14,7 @@ public class IdInterceptor implements Interceptor {
|
||||||
if (id != null && !id.isEmpty()) {
|
if (id != null && !id.isEmpty()) {
|
||||||
inv.invoke();
|
inv.invoke();
|
||||||
} else {
|
} else {
|
||||||
inv.getController().renderError(400);
|
((Controller)inv.getController()).renderError(400, "id must not be null");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package cn.cloudowr.sdk;
|
package cn.cloudowr.sdk.jfinal;
|
||||||
|
|
||||||
import com.jfinal.aop.Interceptor;
|
import com.jfinal.aop.Interceptor;
|
||||||
import com.jfinal.aop.Invocation;
|
import com.jfinal.aop.Invocation;
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package cn.cloudowr.sdk.jfinal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyf66 on 2017/2/20.
|
||||||
|
*/
|
||||||
|
public interface RestfulStyle {
|
||||||
|
void index();
|
||||||
|
void show();
|
||||||
|
void save();
|
||||||
|
void update();
|
||||||
|
void delete();
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue