|
30 | 30 | import static apijson.framework.APIJSONConstant.VISITOR_;
|
31 | 31 | import static apijson.framework.APIJSONConstant.VISITOR_ID;
|
32 | 32 |
|
| 33 | +import java.lang.reflect.Method; |
33 | 34 | import java.rmi.ServerException;
|
34 | 35 |
|
| 36 | +import javax.servlet.AsyncContext; |
| 37 | +import javax.servlet.ServletResponse; |
| 38 | +import javax.servlet.http.HttpServletRequest; |
35 | 39 | import javax.servlet.http.HttpSession;
|
36 | 40 |
|
37 | 41 | import com.alibaba.fastjson.JSONObject;
|
|
42 | 46 | import apijson.StringUtil;
|
43 | 47 | import apijson.orm.Parser;
|
44 | 48 | import apijson.orm.Visitor;
|
| 49 | +import unitauto.MethodUtil; |
| 50 | +import unitauto.MethodUtil.InterfaceProxy; |
45 | 51 |
|
46 | 52 |
|
47 |
| -/**request controller |
| 53 | +/**APIJSON base controller,建议在子项目被 @RestController 注解的类继承它或通过它的实例调用相关方法 |
48 | 54 | * <br > 全通过 HTTP POST 来请求:
|
49 | 55 | * <br > 1.减少代码 - 客户端无需写 HTTP GET, HTTP PUT 等各种方式的请求代码
|
50 | 56 | * <br > 2.提高性能 - 无需 URL encode 和 decode
|
@@ -223,12 +229,44 @@ public Object logout(@NotNull HttpSession session) {
|
223 | 229 | }
|
224 | 230 |
|
225 | 231 |
|
226 |
| - public JSONObject invokeMethod(String request) { |
227 |
| - return MethodUtil.invokeMethod(request); |
228 |
| - } |
229 | 232 |
|
230 | 233 | public JSONObject listMethod(String request) {
|
231 | 234 | return MethodUtil.listMethod(request);
|
232 | 235 | }
|
| 236 | + |
| 237 | + public void invokeMethod(String request, HttpServletRequest servletRequest) { |
| 238 | + AsyncContext asyncContext = servletRequest.startAsync(); |
| 239 | + |
| 240 | + MethodUtil.Listener<JSONObject> listener = new MethodUtil.Listener<JSONObject>() { |
| 241 | + |
| 242 | + @Override |
| 243 | + public void complete(JSONObject data, Method method, InterfaceProxy proxy, Object... extras) throws Exception { |
| 244 | + ServletResponse servletResponse = asyncContext.getResponse(); |
| 245 | + if (servletResponse.isCommitted()) { |
| 246 | + Log.w(TAG, "invokeMethod listener.complete servletResponse.isCommitted() >> return;"); |
| 247 | + return; |
| 248 | + } |
| 249 | + |
| 250 | + servletResponse.setCharacterEncoding(servletRequest.getCharacterEncoding()); |
| 251 | + servletResponse.setContentType(servletRequest.getContentType()); |
| 252 | + servletResponse.getWriter().println(data); |
| 253 | + asyncContext.complete(); |
| 254 | + } |
| 255 | + }; |
| 256 | + |
| 257 | + try { |
| 258 | + MethodUtil.invokeMethod(request, null, listener); |
| 259 | + } |
| 260 | + catch (Exception e) { |
| 261 | + Log.e(TAG, "invokeMethod try { JSONObject req = JSON.parseObject(request); ... } catch (Exception e) { \n" + e.getMessage()); |
| 262 | + try { |
| 263 | + listener.complete(MethodUtil.JSON_CALLBACK.newErrorResult(e)); |
| 264 | + } |
| 265 | + catch (Exception e1) { |
| 266 | + e1.printStackTrace(); |
| 267 | + asyncContext.complete(); |
| 268 | + } |
| 269 | + } |
| 270 | + } |
233 | 271 |
|
234 | 272 | }
|
0 commit comments