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中獲取參數");
    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM