springboot 接收post、get、重定向,並從url中獲取參數


一、請求方式

1、Post請求

    @RequestMapping(value = "/post", method = {RequestMethod.POST})
    public void testPost(@RequestBody String param) {
        System.out.println("POST請求");
    }

2、Get請求

    @RequestMapping(value = "/get", method = {RequestMethod.GET})
    public void testGET(@RequestParam(value = "param")String param) {
        System.out.println("GET請求");

    }

3、重定向(GET請求)

    @RequestMapping(value = "/response", method = {RequestMethod.GET})
    public void testResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("測試重定向");
        response.sendRedirect("http://www.baidu.com");
    }

4、從Url中獲取參數(GET請求)

    @RequestMapping(value = "/{url}", method = {RequestMethod.GET})
    public void testUrl(@PathVariable(value = "url")String url)   {
        System.out.println("從Url中獲取參數");
    }

二、完整代碼

import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/test")
public class test12 {

    /**
     * 1、POST請求獲取參數
     * @param param
     */
    @RequestMapping(value = "/post", method = {RequestMethod.POST})
    public void testPost(@RequestBody String param) {
        System.out.println("POST請求");
    }

    /**
     * 2、GET請求獲取參數
     * @param param
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET})
    public void testGET(@RequestParam(value = "param")String param) {
        System.out.println("GET請求");
    }

    /**
     * 3、GET請求,並重定向
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/response", method = {RequestMethod.GET})
    public void testResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("測試重定向");
        response.sendRedirect("http://www.baidu.com");
    }

    /**
     * 4、從url地址中獲取參數
     * @param url
     */
    @RequestMapping(value = "/{url}", method = {RequestMethod.GET})
    public void testUrl(@PathVariable(value = "url")String url)   {
        System.out.println("從Url中獲取參數");
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章