解析url

本文展示了如何在Spring MVC中处理HTTP请求参数,包括普通方式和RESTful风格。`CarController.java`中演示了不同参数接收方式,如通过对象封装、基本类型和引用类型。`CarController2.java`则介绍了使用`@PathVariable`注解实现RESTful API,以更简洁的方式获取路径变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CarController.java

package cn.tedu.controller;

import cn.tedu.pojo.Car;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//接受请求,并把json数据返回
@RequestMapping("car")//规定了url地址的写法
public class CarController {
    //springmvc框架解析请求中的参数
    @RequestMapping("get5")
    //http://localhost:8091/car/get5?id=10&name=BMW&price=9.9
    public void get5(Car c){//springmvc框架会把请求的参数,封装给car对象
        System.out.println(c.getId()+c.getName()+c.getPrice());
    }
    //http://localhost:8091/car/get4?id=10&name=BMW
    @RequestMapping("get4")
    public void get4(Integer id,String name){
        //id是用来接受url里id的值,name用来接受url里name的值
        System.out.println(id+name);
    }
    //http://localhost:8091/car/get3?id=10
    @RequestMapping("get3")
    //public void get3(int id){//参数是基本类型,访问这个方法必须带参数,否则有异常
    public void get3(Integer id){//参数是引用类型,访问这个方法没带参数就是null
        System.out.println(id);
        }
        //自己解析请求中的参数
        public void get2(){
        String url="http://localhost:8091/car/get2?id=10&name=BMW&price=9.9";
            String[] s = url.split("\\?")[1].split("&");
            for(String ss:s){
                String key=ss.split("=")[0];
                String value=ss.split("=")[1];
            }
        }

        public Car get(){
            Car c = new Car(10,"BMW",19.9);
            return c;
        }
}

CarController2.java

package cn.tedu.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*对比请求参数的不同获取方式*/
@RestController
@RequestMapping("car2")
public class CarController2 {
    //1普通的get方式获取请求参数
    //http://localhost:8091/car2/get?id=10&name=BMW&age=10&sex=1
    @RequestMapping("get")
    public String get(Integer id,String name,Integer age,Integer sex){
       //return id+name+age+sex;
       //return id+name+age+sex;//组织成json串给浏览器展示
        return "{"+"id"+":"+id+"}";//{id:10}
    }
    //2.restful方式获取请求参数:通过{}绑定地址中参数的位置+通过注解获取{???}的值
    //restful简化get提交的数据的写法
    //http://localhost:8091/car2/get2/10/BMW/10/1
    @RequestMapping("get2/{id}/{name}/{x}/{y}")
    public void get2(@PathVariable Integer id,
                     @PathVariable String name,
                     @PathVariable String x,
                     @PathVariable Integer y ){
        System.out.println(id);
        System.out.println(name);
        System.out.println(x);
        System.out.println(y);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值