1.3版本前整合gwt spring,用到了第三方类,如cglib扩展类具备gwt servlet的功能,随着1.4版本的发布(发布很久了T_T '),整合有了新的方式,闲话不说,看看整合后的servlet如何调用服务:
java 代码
- /**
- * GWTRemoteServiceServlet act as a dispatch servlet for all GWT services
- *
- * in your web.xml, mapping all request /gwtrpc/ to this servlet
- *
- */
- public class GWTRemoteServiceServlet extends RemoteServiceServlet {
- private WebApplicationContext springContext;
- @Override
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- springContext = (WebApplicationContext) config
- .getServletContext()
- .getAttribute(
- WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
- if (springContext == null) {
- throw new RuntimeException(
- "Check your web.xml setting, no Spring context configured");
- }
- }
- @Override
- protected void service(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- try {
- HttpRequestContext context = new HttpRequestContext(req, resp, this
- .getServletConfig());
- HttpRequestContext.ThreadLocalHttpRequestContext.set(context);
- ServletContext servletContext = getServletContext();
- perThreadRequest.set(req);
- perThreadResponse.set(resp);
- String pathInfo = req.getPathInfo();
- if (pathInfo.startsWith("/")) {
- pathInfo = pathInfo.substring(1);
- }
- RemoteService service = (RemoteService) springContext.getBean(pathInfo);
- String requestPayload = readPayloadAsUtf8(req);
- // Let subclasses see the serialized request.
- //
- onBeforeRequestDeserialized(requestPayload);
- // Invoke the core dispatching logic, which returns the serialized
- // result.
- //
- String responsePayload = processCall(service, requestPayload);
- // Let subclasses see the serialized response.
- //
- onAfterResponseSerialized(responsePayload);
- // Write the response.
- //
- writeResponse(req, resp, responsePayload);
- } catch (Throwable e) {
- // Give a subclass a chance to either handle the exception or
- // rethrow it
- //
- doUnexpectedFailure(e);
- } finally {
- HttpRequestContext.ThreadLocalHttpRequestContext.remove();
- }
- }
- /**
- * rewrite processCall
- *
- * @param bean
- * @param payload
- * @return
- * @throws SerializationException
- */
- public String processCall(RemoteService bean, String payload)
- throws SerializationException {
- try {
- RPCRequest rpcRequest = RPC.decodeRequest(payload, bean.getClass(),
- this);
- return RPC.invokeAndEncodeResponse(bean, rpcRequest.getMethod(),
- rpcRequest.getParameters(), rpcRequest
- .getSerializationPolicy());
- } catch (IncompatibleRemoteServiceException ex) {
- getServletContext()
- .log(
- "An IncompatibleRemoteServiceException was thrown while processing this call.",
- ex);
- return RPC.encodeResponseForFailure(null, ex);
- }
- }
- }
xml代码:
- <bean id="bookservice" class="com.service.BookService" />
java 代码
- class BookService implements IBookService{
- }
- interface IBookService extends RemoteService{
- }
ok,该servlet继承RemoteServiceServlet,做了三件事
1.init时获得spring webContext
2.service时解析请求字符,如/service/bookservice,获得bookservice名,对应spring bean id
3.重写processCall方法,通过bookservice,在springContext中获得注册了的bookservice,提供调用