Atitit atimvc rest原理与自定义实现t33
目录
原理 过滤器 过滤即可
/tomcatx/src/restpkg/jerserMeth.java
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>js_jsp_utf8</display-name>
<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>
<!--用于定义 RESTful Web Service 接口-->
<servlet>
<servlet-name>JerseyServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>restpkg.jerseyCfg</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
package restpkg;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.reflect.ConstructorUtils;
import com.attilax.mvc.MethodObj;
import com.attilax.mvc.MvcUtil;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
//@Component
@WebFilter(urlPatterns = "/*", filterName = "mvcFilter")
public class MvcFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) {
try {
// get url out mapper
List list = Lists.newArrayList();
list.add(jerserMeth.class);
Map<String, MethodObj> url_method_maps = MvcUtil.get_url_out_mapper(list);
HttpServletRequest httpServletRequest = (HttpServletRequest) arg0;
HttpServletResponse httpServletResponse = (HttpServletResponse) arg1;
String uri = httpServletRequest.getRequestURI();
System.out.println(uri);
if (url_method_maps.get(uri) == null) {
arg2.doFilter(arg0, arg1);
return;
}
MethodObj MethodObj1 = url_method_maps.get(uri);
Object classObj = ConstructorUtils.invokeConstructor(MethodObj1.classProp, null);
Method meth = MethodObj1.methodProp;
Object ivkrzt = meth.invoke(classObj, httpServletRequest, httpServletResponse);
// outputHtml(httpServletResponse, r);
System.out.println("==ivkrzt:" + ivkrzt);
return; // not to next ,beirs springmvc show err,cause cant this mappering
// arg2.doFilter(arg0, arg1);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// private List getMycomponentClassList() {
// List li_allclass=Lists.newArrayList();
// li_allclass.add(MvcCtrol.class);
//
// List li= Lists.newArrayList();
// classList.add();
//
// }
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
package restpkg;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import com.attilax.mvc.MvcUtil;
public class jerserMeth {
// http://localhost/services/t?aaa
@Path("/t")
public String get(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
MvcUtil.outputHtml(httpServletResponse, "hhh");
return "thing";
}
}