Spring MVC 使用HiddenHttpMethodFilter配置Rest风格的URL

 

RESTFUL格式


/**
        Rest 风格的 URL. 
        以 CRUD 为例: 
         新增: /order POST 
         修改: /order/1 PUT update?id=1 
         获取:/order/1 GET get?id=1 
         删除: /order/1 DELETE delete?id=1
      
     如何发送 PUT 请求和 DELETE 请求呢 ? 
        1. 需要在web.xml文件中配置 HiddenHttpMethodFilter 
        <!-- 
            配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 POST 请求 
        -->
        <filter>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        
        <filter-mapping>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
        2. 需要发送 POST 请求
        3. 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT
        jsp文件如下:
        <form action="springmvc/testRest/1" method="post">
        <input type="hidden" name="_method" value="PUT"/>
        <input type="submit" value="TestRest PUT"/>
        </form>
        <br><br>
    
        <form action="springmvc/testRest/1" method="post">
            <input type="hidden" name="_method" value="DELETE"/>
            <input type="submit" value="TestRest DELETE"/>
        </form>
        <br><br>
        
        <form action="springmvc/testRest" method="post">
            <input type="submit" value="TestRest POST"/>
        </form>
        <br><br>
        
        <a href="springmvc/testRest/1">Test Rest Get</a>
        <br><br>
      
      在 SpringMVC 的目标方法中如何得到 id 呢? 使用 @PathVariable 注解
     
     */
    @RequestMapping(value = "/testRest/{id}", method = RequestMethod.PUT)
    public String testRestPut(@PathVariable Integer id) {
        System.out.println("testRest Put: " + id);
        return SUCCESS;
    }

    @RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
    public String testRestDelete(@PathVariable Integer id) {
        System.out.println("testRest Delete: " + id);
        return SUCCESS;
    }

    @RequestMapping(value = "/testRest", method = RequestMethod.POST)
    public String testRest() {
        System.out.println("testRest POST");
        return SUCCESS;
    }

    @RequestMapping(value = "/testRest/{id}", method = RequestMethod.GET)
    public String testRest(@PathVariable Integer id) {
        System.out.println("testRest GET: " + id);
        return SUCCESS;
    }

    /**
     * @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中.
     * @param id
     * @return
     */
    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable("id") Integer id) {
        System.out.println("testPathVariable: " + id);
        return SUCCESS;
    }

    @RequestMapping("/testAntPath/*/abc")
    public String testAntPath() {
        System.out.println("testAntPath");
        return SUCCESS;
    }

 

URL的匹配规则

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @作者 Mitkey
 * @时间 2017年1月19日 下午3:31:26
 * @类说明:
 * @版本 xx
 */
@SpringBootApplication
public class Application extends WebMvcConfigurationSupport {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
  /**
   * 1、 extends WebMvcConfigurationSupport
   * 2、重写下面方法;
   * setUseSuffixPatternMatch : 设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配;
   * setUseTrailingSlashMatch : 设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配;
   */
    @Override
    protected void configurePathMatch(PathMatchConfigurer configurer) {
        super.configurePathMatch(configurer);
        // 常用的两种
        // 匹配结尾 / :会识别 url 的最后一个字符是否为 /
        // localhost:8080/test 与 localhost:8080/test/ 等价
        configurer.setUseTrailingSlashMatch(true);
        // 匹配后缀名:会识别 xx.* 后缀的内容
        // localhost:8080/test 与 localhost:8080/test.jsp 等价
        configurer.setUseSuffixPatternMatch(true);

        // TODO PathMatchConfigurer 还提供其他的一些 api 以供使用
    }

}

以上代码有两句核心的代码:

setUseSuffixPatternMatch(boolean useSuffixPatternMatch):

设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配;

当此参数设置为true的时候,那么/user.html,/user.aa,/user.*都能是正常访问的。

当此参数设置为false的时候,那么只能访问/user或者/user/( 这个前提是setUseTrailingSlashMatch 设置为true了)。

setUseTrailingSlashMatch (boolean useSuffixPatternMatch):

设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配;

当此参数设置为true的会后,那么地址/user,/user/都能正常访问。

当此参数设置为false的时候,那么就只能访问/user了。

当以上两个参数都设置为true的时候,那么路径/user或者/user.aa,/user.*,/user/都是能正常访问的,但是类似/user.html/ 是无法访问的。

当都设置为false的时候,那么就只能访问/user路径了。

 

 

 

 

转载于:https://my.oschina.net/spinachgit/blog/1647608

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值