zoukankan      html  css  js  c++  java
  • jersey 过滤器

    这里使用的Jersey 是 1.1 版本

    1.  web.xml 配置

    <?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" 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>restDemo</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>
      
         <servlet>
    	  <servlet-name>Jersey REST Service</servlet-name>
    	<servlet-class>
    	  com.sun.jersey.spi.container.servlet.ServletContainer
    	</servlet-class>
    	  <init-param>
    	    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>  
              <param-value>com.mtour.rest.resources.requestFilter</param-value> 
    	  </init-param>
    	  <load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    	  <servlet-name>Jersey REST Service</servlet-name>
    	  <url-pattern>/rest/*</url-pattern>
    	</servlet-mapping>
      
    </web-app>
    

    2.  新建 类 requestFilter

    package com.mtour.rest.resources;
    
    import javax.ws.rs.WebApplicationException;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.core.Response.Status;
    
    import com.sun.jersey.spi.container.ContainerRequest;
    import com.sun.jersey.spi.container.ContainerRequestFilter;
    
    public class requestFilter implements ContainerRequestFilter{
    
    	@Override
    	public ContainerRequest filter(ContainerRequest arg0) {
    		// TODO Auto-generated method stub
    		
    		throw new WebApplicationException(
                    Response.status(Status.INTERNAL_SERVER_ERROR).build());
    		
    	}
    	
    }
    

    当 请求过来的时候首先到达这里,这里做了测试 直接返回500 错误

    抓包查看返回

  • 相关阅读:
    分治法求最大子序列
    6.2 链表 (UVa 11988, 12657)
    6.1 栈和队列 (UVa 210, 514, 442)
    S-Tree (UVa 712) 二叉树
    Equilibrium Mobile (UVa 12166) dfs二叉树
    Patrol Robot (UVa 1600) BFS
    Knight Moves (UVa 439) BFS
    Tree Recovery (UVa 536) 递归遍历二叉树
    Parentheses Balance (Uva 673) 栈
    Self-Assembly (UVa 1572)
  • 原文地址:https://www.cnblogs.com/mtour/p/5744516.html
Copyright © 2011-2022 走看看