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中获取参数");
    }

}
posted @ 2020-03-06 18:14  ah_lydms  阅读(2877)  评论(0编辑  收藏  举报