springboot 接收post、get、重定向,并从url中获取参数

简介: springboot 接收post、get、重定向,并从url中获取参数

@[toc]


一、请求方式

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

}
目录
相关文章
|
2月前
|
存储 Java API
在springboot中缩短一个url链接
URL缩短服务是现代应用中常见的需求,用于将长URL映射为简短的唯一代码,便于分享。该服务具备多种功能,如自动过期、访问统计、防止重复及安全机制。通过Spring Boot构建RESTful API,使用H2数据库存储数据,Java UUID生成短码,并通过定时任务清理过期URL。用户可通过API提交长URL获取短链接,查询访问量,系统会自动重定向并记录访问次数。每天午夜自动清理过期URL,确保数据整洁。此项目结构清晰,涵盖实体类、Repository、Service和Controller等核心组件,适合快速开发和扩展。
|
3月前
|
JavaScript 前端开发 Java
SpringBoot项目的html页面使用axios进行get post请求
SpringBoot项目的html页面使用axios进行get post请求
75 2
|
3月前
|
JavaScript 前端开发 Java
SpringBoot项目的html页面使用axios进行get post请求
SpringBoot项目的html页面使用axios进行get post请求
52 0
|
5月前
|
缓存 前端开发 Java
springboot 的单体服务 字典参数转译
本文介绍了如何在Spring Boot项目中使用缓存来管理字典参数,并确保前后端数据一致性。首先,通过`@EnableCaching`启用缓存功能,接着创建一个自定义的字典缓存类`DicCache`。然后,通过配置类将`DicCache`添加到`cacheManager`中。此外,对字典服务进行改造,使用`@CachePut`和`@CacheEvict`注解保证数据一致性。最后,实现自定义注解`@DicSerializer`和序列化处理类`DictSerializerHandel`,用于在序列化过程中自动转换字典值。通过这种方式,可最小化代码改动并提高系统性能。
springboot 的单体服务 字典参数转译
|
4月前
|
JSON NoSQL Java
springBoot:jwt&redis&文件操作&常见请求错误代码&参数注解 (九)
该文档涵盖JWT(JSON Web Token)的组成、依赖、工具类创建及拦截器配置,并介绍了Redis的依赖配置与文件操作相关功能,包括文件上传、下载、删除及批量删除的方法。同时,文档还列举了常见的HTTP请求错误代码及其含义,并详细解释了@RequestParam与@PathVariable等参数注解的区别与用法。
|
4月前
url重写重定向所有http网址到https网址
url重写重定向所有http网址到https网址
54 4
|
3月前
|
Java Spring
JAVA获取重定向地址URL的两种方法
【10月更文挑战第17天】本文介绍了两种在Java中获取HTTP响应头中的Location字段的方法:一种是使用HttpURLConnection,另一种是使用Spring的RestTemplate。通过设置连接超时和禁用自动重定向,确保请求按预期执行。此外,还提供了一个自定义的`NoRedirectSimpleClientHttpRequestFactory`类,用于禁用RestTemplate的自动重定向功能。
205 0
|
5月前
|
JavaScript 前端开发 Java
SpringBoot项目的html页面使用axios进行get post请求
SpringBoot项目的html页面使用axios进行get post请求
65 6
|
5月前
|
JSON Java 数据格式
springboot 参数统一处理
springboot 参数统一处理
|
4月前
|
监控 Java Maven
springboot学习二:springboot 初创建 web 项目、修改banner、热部署插件、切换运行环境、springboot参数配置,打包项目并测试成功
这篇文章介绍了如何快速创建Spring Boot项目,包括项目的初始化、结构、打包部署、修改启动Banner、热部署、环境切换和参数配置等基础操作。
239 0