springboot 接收post、get、重定向,并从url中获取参数
文章目录一、请求方式1、Post请求2、Get请求3、重定向(GET请求)4、从Url中获取参数(GET请求)二、完整代码一、请求方式1、Post请求@RequestMapping(value = "/post", method = {RequestMethod.POST})public void testPost(@RequestBody String param) {...
·
一、请求方式
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中获取参数");
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)