栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot请求注解的使用Get/Post

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot请求注解的使用Get/Post

一下是主要是@GetMapping的使用

package com.zjh.hellospringboot.controller;

import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import java.util.Map;

@RestController
public class MyController {
    @RequestMapping("/1.jpg")
    public String mytest01(){
        return "你的html名字";
    }

    @GetMapping("/get/{reset}")
    public String getRequest01(@PathVariable("reset") String param01, @RequestParam("param2") String param02,
                               @RequestHeader("User-Agent") String header,@CookieValue("Idea-5574819b") String cookie
                               ){
        //@PathVariable是对restful风格的请求参数的提取他可以通过在注解的()内指定url中{}内的参数http://localhost:8080/get/参数1
        //@RequestParam是对url路径?后面的的get方式的参数进行取值 http://localhost:8080/get/参数1?param2=参数2
        //@RequestHeader可以对请求中的请求头信息进行提取可以通过()内的参数指定
        //以上注解除了可以单个取值外也可以进行整合取值
        //比如他们都可以将各自的注解进行Map整合取值,但是注意使用map map的kv 必须都是String才能自动填入kv
        //@cookievalue参数必须是已有的正确cookie键名
        //@GetMapping无法映射类参数
        return param01+param02+header+"||";
    }
    @GetMapping("/get2/{reset}/{io}")
    public String getRequest02(@PathVariable Map pv, @RequestParam Map pm, @RequestHeader Map rh
    ,@CookieValue("Idea-5574819b") Cookie cookie
                               ){
        //http://localhost:8080/get2/er/er/?name=啊这&path=2
        //http://localhost:8080/get2/reset/io?name=你好
        //@cookievalue参数必须是已有的正确cookie键名
        return pv.toString()+"|"+pm.toString()+"|"+rh.toString()+"||"+cookie;
    }

    @PostMapping("/postT")
    public String getRequest03(@RequestBody String Content){
        //@RequestBody 这个注解可以将提交的表单数据进行一个封装并全部转化为字符串
        return "";
    }
}

一下是对请求转发的简单使用

@Controller
public class FowardController {
    @GetMapping("/goto")
    public String re1(HttpServletRequest httpServletRequest){
        httpServletRequest.setAttribute("msg","你好");
        //转发可以将请求域中的参数发送到另一个页面使用且url不变, 重定向不行

        return "forward:/here";
    }
    @GetMapping("/here")
    @ResponseBody
    public String re2(@RequestAttribute("msg") String content){
        //@RequestAttribute可以指定一个请求域中的键来获取他的至,但是他不支持集合取值,只能单个取值
        return content;
    }
}

一下是对矩阵变量的使用,矩阵变量不仅要配置controller还要去配置springboot的配置类配配置urlpathmatch

package com.zjh.hellospringboot.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class JuZhenBianLiang {
    //矩阵变量是以https://space.bilibili.com/1;name=sf;hobby=1,2;/2;name=sf;hobby=1,2;
    //他是以/1;name=sf;hobby=1,2; 其中以1后的;为路径结尾后面的为变量名值;为结尾 / 后是下一个路径变量来存储其他的矩阵变量
    //矩阵变量是绑定 在路径变量中的也就是restful风格所使用的@pathvariable 注解中的
    //若只有一个路径变量 矩阵变量指定的变量也就不用指定是哪个路径变量中的矩阵变量
    //但是矩阵变量需要在springboot配置类中配置关闭 ->移除路径分号内容 默认是开启的  需要在配置类实现接口WebMvcConfigurer
    //的方法configurePathMatch
    @GetMapping("/{lidakai}")//单路径多变量
    public String SingleVariable(@PathVariable("lidakai") String path, @MatrixVariable("name") String name){
        return path+"|"+name;
    }
    @GetMapping("/{lidakai}/{op}")
    public String multiVariable(@PathVariable("lidakai") String lidakai,@PathVariable("op") String c
    ,@MatrixVariable(value = "name",pathVar = "lidakai") String name1,@MatrixVariable(value = "name",pathVar = "op") String name2
    ){
        return name1+"|"+name2;
    }
}
package com.zjh.hellospringboot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

@Configuration
public class PathConfig implements WebMvcConfigurer {
    //如要网站禁用cookie后   我们的session会存放在cookie中的jsessionid中  我们便无法使用session
    //我们可以使用矩阵变量来传值
    //此类是用来开启url矩阵变量的一种方法
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper ul= new UrlPathHelper();
        ul.setRemoveSemicolonContent(false);//设置移除分号内容 原为true 现在设置为false
        configurer.setUrlPathHelper(ul);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831248.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号