------ Session包装类,方便在Rest业务类等无状态类中使用(有点违背原则,但是目前我们的验证方案需要如此,又不想影响大部分验证无关的业务)
使用如下:
HttpSession session = RequestFilter.threadLocal.get().getSession();
import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class RequestFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } public static ThreadLocal<HttpServletRequest> threadLocal = new ThreadLocal<HttpServletRequest>(); @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub threadLocal.set((HttpServletRequest) arg0); arg2.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
------ 把Spring上下文放入静态变量,方便以后使用
import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SpringInitWrap extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public void init(){ WebApplicationContext wac =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); ServletContext a= wac.getServletContext(); SpringDTO.setCtx(wac); } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
------ 配置Rest
根据自定义的接口在Spring上下文寻找Rest业务类
import javax.ws.rs.core.Application; public class TyacRestApp extends Application { // @Override // public Set<Class<?>> getClasses() { // Set<Class<?>> classes = new HashSet<Class<?>>(); // classes.add(TyacServiceResource.class); // classes.add(VMServiceResource.class); // classes.add(SrvResource.class); // classes.add(PCSrvResource.class); // classes.add(ChkSrv.class); // // return classes; // } @Override public Set<Object> getSingletons() { String beans[] = SpringDTO.getCtx().getBeanNamesForType(IREST.class); Set<Object> result = new HashSet<Object>(); if (beans != null) { for (int i = 0; i < beans.length; i++) { String beanName = beans[i]; result.add(SpringDTO.getCtx().getBean(beanName)); } } return result; } }
Rest业务类示范
@Path("/pcsrv") public class PCSrvRest implements IREST { @GET @Path("list/pc") @Produces(MediaType.TEXT_PLAIN) public Response listPC(@Context UriInfo uriInfo) { JSONArray jsonArr = null; try { PCMgr pcMgr = SpringDTO.getCtx().getBean("pcMgr", PCMgr.class); List<Pc> list = pcMgr.listPC(-1, -1); jsonArr = JSONArray.fromObject(list); System.out.println(jsonArr); } catch (Exception ex) { ex.printStackTrace(); return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).type(MediaType.TEXT_PLAIN) .entity("Can not read data from server").build(); } return Response.status(HttpURLConnection.HTTP_OK).type(MediaType.TEXT_PLAIN) .entity(jsonArr.toString()).build(); } @GET @Path("apply/vm/{vmtemplate}/{count}/{cpu}/{memory}/{disk}/{hostname}") @Produces(MediaType.TEXT_PLAIN) public Response opApplyVM(@Context UriInfo uriInfo, @PathParam("vmtemplate") String vmtemplate, @PathParam("count") String count, @PathParam("cpu") String cpu, @PathParam("memory") String memory, @PathParam("disk") String disk , @PathParam("hostname") String hostname) { ... } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>cn.tianya.tyac.rest</display-name> <servlet> <description> </description> <display-name>TyacApp</display-name> <servlet-name>TyacApp</servlet-name> <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class> <init-param> <description> </description> <param-name>javax.ws.rs.Application</param-name> <param-value>cn.tianya.tyac.rest.TyacRestApp</param-value> </init-param> <load-on-startup>2</load-on-startup> <enabled>true</enabled> <async-supported>false</async-supported> </servlet> <servlet> <description> </description> <display-name>SpringInitWrap</display-name> <servlet-name>SpringInitWrap</servlet-name> <servlet-class>cn.tianya.tyac.rest.SpringInitWrap</servlet-class> <load-on-startup>1</load-on-startup> <enabled>true</enabled> <async-supported>false</async-supported> </servlet> <servlet-mapping> <servlet-name>TyacApp</servlet-name> <url-pattern> /services/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SpringInitWrap</servlet-name> <url-pattern> /test/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter> <filter-name>RequestFilter</filter-name> <filter-class>cn.tianya.tyac.filter.RequestFilter</filter-class> </filter> <filter-mapping> <filter-name>RequestFilter</filter-name> <url-pattern>/services/*</url-pattern> </filter-mapping> <context-param> <param-name>log4jConfigLocation</param-name> <param-value> /WEB-INF/log4j.xml </param-value> </context-param> </web-app>