• springMVC get请求及其请求地址写法


     今天,需要写一个接口,写完之后,测试的时候发线一直报404错误,不知道为什么报错。应该是get请求地址的问题,get请求有两个参数,改为一个参数的时候是好用的,可能那种方式不适合写两个参数的get请求吧。

      方式一:get请求一个参数

    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable(value="id") Integer id){
        System.out.println("testPathVariable:" + id);
        return SUCCESS;
    }        

       url请求地址: http://localhost:8080/testPathVariable/001

        页面输出:testPatVariable:001

    方式二:get请求多个参数(2个)

    @RequestMapping(value="/testRequestParam")
    public String testRequestParam(@RequestParam(value="username") String username, 
                      @RequestParam(value="age", required=false, defaultValue="0") int age){ System.out.println("testRequestParam" + " username:" + username + " age:" +age); return SUCCESS; }

        url请求地址:http://localhost:8080/testRequestParam?username=jackie&age=12

        页面输出:teatRequestParam username:jackie age:12

        Spring 注解 

      1、spring mvc如何匹配请求路径

        @RequestMapping是用来映射请求的,比如get请求,post请求,或者REST风格与非REST风格的。

        该注解可以用在类上或者方法上,如果用于类上,表示该类中所有方法的父路径。

      2、spring mvc如何获取请求的参数

        @PathVariable该注解用来映射请求URL中绑定的占位符。通过@PathVariable可以将URL中占位符的参数绑定到controller处理方法的入参中

        @RequestParam该注解也是用来获取请求参数的。

        

  • 相关阅读:
    十进制转换成二进制列表
    openssl生成RSA格式的公私钥,并转为pkcs8格式
    yum安装报错:Failure when receiving data from the peer
    springboot配置swagger
    Impala配置HA-Nginx
    如何安装windows7
    MySQL数据实时增量同步到Kafka
    ElasticSearch-SQL 安装和使用
    部署nexus服务
    maven发布jar包到nexus
  • 原文地址:https://www.cnblogs.com/wushifeng/p/6142288.html
Copyright © 2020-2023  润新知