1. 程式人生 > >SpringBoot RequestMapping各方法的使用

SpringBoot RequestMapping各方法的使用

在使用SpringBoot開發web應用時實際採用的是spring mvc來實現,現在採用REST風格的開發方式越來越多spring當然也支援這一開發模式。

程式碼環境spring boot 頁面配置為使用jsp的方式

rest模式我就不介紹了,主要是GET、POST、PUT、DELETE方法,spring mvc也提供了對應的實現方式 
@RequestMapping(method = RequestMethod.GET) 
@RequestMapping(method = RequestMethod.POST) 
@RequestMapping(method = RequestMethod.PUT) 
@RequestMapping(method = RequestMethod.DELETE) 
當然也可以使用 
@GetMapping 
@PostMapping 
@PutMapping 
@DeleteMapping 
這與上面的是一樣的效果 
下面我們來一個一個的實現

GetMapping

    @GetMapping("/httpMethod")
    public String httpMethd(){
        return "test/test";
    }

使用這個程式碼我們定義了一個get方法,通過這個方法spring boot會自動跳轉到test目錄下的test.jsp頁面當然你還可以在@GetMapping下面再加上@ResponseBody,這樣返回的就是一個字串而不是跳轉頁面了。

PostMapping

test.jsp頁面

<%--
  Created by IntelliJ IDEA.
  User: jacky
  Date: 17-10-3
  Time: 下午4:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>HttpMethodTest</title>
</head>
<body>
<form action="/httpMethod" method="post">
    名稱
    <input type="text" name="name">
    <br>
    密碼
    <input type="text" name="pwd">
    <br>
    <input type="submit" name="提交">
</form>
</body>
</html>

controller類

    @PostMapping("/httpMethod")
    @ResponseBody
    public String httpMethod(@RequestParam String name,@RequestParam String pwd){
        System.out.println("sent name is "+name);
        System.out.println("sent pwd is "+pwd);
        return "success";
    }

這裡表單定義了提交方法為post,這時提交表單在後臺頁面就能列印處理提交的資訊,同時會向前臺返回success字串。

PutMapping DeleteMapping

本質上瀏覽器端的form表單只支援GET和POST方法並不支援PUT和DELETE方法,但是spring已經解決了這個問題,從spring3.0開始定義了一個filter來支援對PUT和DELETE方法的解析,下面我們就來看看怎麼處理。 
首先我們要對jsp頁面的表單做一個簡單的修改

<%--
  Created by IntelliJ IDEA.
  User: jacky
  Date: 17-10-3
  Time: 下午4:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>HttpMethodTest</title>
</head>
<body>
<form action="/upload/httpMethod" method="post">
    <input type="hidden" name="_method" value="PUT/DELETE">
    名稱
    <input type="text" name="name">
    <br>
    密碼
    <input type="text" name="pwd">
    <br>
    <input type="submit" name="提交">
</form>
</body>
</html>

對比一下就看到了吧,我們添加了一個隱藏的輸入框名字叫_method,值為PUT或者是DELETE 
controller程式碼

    @PutMapping("/httpMethod")
    @ResponseBody
    public String httpMethodPut(@RequestParam String name,@RequestParam String pwd){
        System.out.println("put sent name is "+name);
        System.out.println("put sent pwd is "+pwd);
        return "success";
    }

    @DeleteMapping("/httpMethod")
    @ResponseBody
    public String httpMethodDel(@RequestParam String name,@RequestParam String pwd){
        System.out.println("delete sent name is "+name);
        System.out.println("delete sent pwd is "+pwd);
        return "success";
    }

下面是重點了,引入filter,直接看程式碼

@Configuration
@ImportResource({"classpath:applicationContext.xml"})
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MaterialApplication.class);
    }

    @Bean
    public FilterRegistrationBean httpMethodFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(httpMethodFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("HttpMethodFilter");
        registrationBean.setOrder(1);
        return registrationBean;
    }

    @Bean
    public Filter httpMethodFilter(){
        return new HiddenHttpMethodFilter();
    }

核心的filter就是HiddenHttpMethodFilter,這是spring自己定義來處理put和delete請求的filter,只需要配置這個filter過濾所有請求就行了,當然你還可以考慮過濾指定的路徑請求。 
配置好後就可以測試一下結果。