Jersey采用JSP模板
程序员文章站
2022-06-06 09:49:43
...
Jersey作为Oracle的rest架构的开源框架,它既然是servlet,那么它也同样支持跟页面的交互,这里我采用了JSP作为模板来体现跟web page的交互,当然它也支持freemarker、velocity等模板引擎。
工程采用maven构建,第一步maven需要引入的lib包
<dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <!-- use the following artifactId if you don't need servlet 2.x compatibility --> <!-- artifactId>jersey-container-servlet</artifactId --> </dependency> <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-mvc-jsp</artifactId> </dependency> <!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> </dependency> --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>xalan</groupId> <artifactId>xalan</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.11.0</version> </dependency>
第二步:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- This web.xml file is not required when using Servlet 3.0 container, see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html --> <web-app version="2.5" 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_2_5.xsd"> <display-name>vote</display-name> <!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> --> <filter> <filter-name>AuthFilter</filter-name> <filter-class>org.ewell.filter.AuthFilter</filter-class> </filter> <filter> <filter-name>jersey</filter-name> <filter-class>com.agenda.base.AppServletContainer</filter-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.agenda.AgendaApplication</param-value> </init-param> <init-param> <param-name>javax.servlet.Filter</param-name> <param-value>org.ewell.filter.AuthFilter</param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name> <param-value>/.*</param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name> <param-value>/</param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.feature.Redirect</param-name> <param-value>true</param-value> </init-param> <!-- pass to next filter if Jersey/App returns 404 --> <init-param> <param-name>jersey.config.servlet.filter.forwardOn404</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>jersey</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>AuthFilter</filter-name> <url-pattern>/rest/*</url-pattern> </filter-mapping> </web-app>
第三步:
ResourceConfig的配置
package com.agenda; import org.ewell.filter.AuthFilter; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature; public class AgendaApplication extends ResourceConfig { public AgendaApplication() { property("jersey.config.server.mvc.templateBasepath.jsp", ""); // Resources. packages(ContactosResource.class.getPackage().getName()); // MVC. register(JspMvcFeature.class); // Logging. // register(LoggingFilter.class); // register(AuthFilter.class); } }
第四步:
独立的ServletContainer
package com.agenda.base; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.ewell.model.basic.RequestThreadLocal; import org.ewell.model.basic.RequestUtil; import org.glassfish.jersey.servlet.ServletContainer; public class AppServletContainer extends ServletContainer { private static final long serialVersionUID = -766501864493188353L; @Override public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; RequestThreadLocal reqThreadLocal = RequestUtil .bindRequest(httpRequest); String requestURI = httpRequest.getRequestURI(); System.out.println("AppServletContainer" + requestURI); if ((requestURI.contains("login")) || keepAliveUser(reqThreadLocal, requestURI) || !(requestURI.contains("rest"))) { super.doFilter(request, response, chain); } else { HttpServletResponse httpResponse = (HttpServletResponse) response; // httpResponse.sendRedirect("/vote/index.html"); super.doFilter(request, response, chain); } } private boolean keepAliveUser(RequestThreadLocal reqThreadLocal, String requestURI) { boolean isAlive = (requestURI.contains("rest")) && (reqThreadLocal.getUser() != null); System.out.println("isAlive" + isAlive); return isAlive; } }
最后一步:
Resource的编写
package com.agenda.rest; import java.util.ArrayList; import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.ewell.model.User; import org.ewell.model.basic.RequestUtil; import org.glassfish.jersey.server.mvc.Template; import org.glassfish.jersey.server.mvc.Viewable; import org.springframework.stereotype.Component; import com.agenda.Contacto; import com.agenda.ContactosRepository; import com.agenda.base.BaseResource; @Component @Path("/rest/users") public class UserResource extends BaseResource { @GET @Path("/login") @Template @Produces(MediaType.TEXT_HTML) public Viewable login() { User user = new User(); RequestUtil.setOperator(getHttpRequest(), user); return new Viewable("/index", null); } }
至于index.jsp页面可以JSTL标记语言编写,跟其他servlet的框架一样
结论:
jersey其实在中国国内并不多见,主要国内采纳的是springMVC或者struts2等框架,但是我个人觉得如果你想学习rest的风格,我强烈推荐jersey framework,spring或者struts2的注解都特别的复杂,很难把控住,jersey比较通俗易懂,缺点也很明显,就是相关的中文文档偏少,不过貌似ITEYE中有位博主,忘记姓名了;第二它自身存在的servlet通病,并发性较差;第三步相较于springMVC,它体现出来的MVC思想感觉没体现。
下一篇: 慢性胃炎的食疗方子