springboot请求注解的使用GetPost

springboot请求注解的使用GetPost,第1张

一下是主要是@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);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/723717.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存