SpringBoot传参方式

地址传参

1.创建一个Action类

package com.lion.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**2019年08月05日 15时28分02秒 **
 * 目的:进行地址重写传参
 * 运行结果:springboot启动后在浏览器访问: http://localhost:8080/?msg=hello
 * 得到:【ECHO】hello
 * 总结:springboot极大的减少了原有的SpringMVC的配置量
 * */
@Controller
public class MessageAction {
    @ResponseBody
    @RequestMapping("/")
    public String echo(String msg){
        return "【ECHO】" + msg ;
    }
}

2.在浏览器访问: http://localhost:8080/?msg=hello    加上参数

Rest风格参数传递

package com.lion.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**2019年08月05日 15时28分02秒 **
 * 目的:Rest传参
 * 运行结果:springboot启动后在浏览器访问: http://localhost:8080/hello
 * 得到:【ECHO】hello
 * 总结: @PathVariable 将 @RequestMapping 中value带的变量 此处是 {id}与方法中参数绑定
 * 注意:如果变量名称与方法参数名称不一致,则需要指定
 * REST缺点 : 跳转的时候浏览器不认post/get 之外的访问方法
 * 优点:可以限制接收提交的方式,有利于规范代码。
 * */
@Controller
public class MessageAction {
    @ResponseBody
    @RequestMapping("/{message}")
    public String echo(@PathVariable("message")String msg){
        return "【ECHO】" + msg ;
    }
}

 

posted @ 2019-08-05 16:45  萧余  阅读(3308)  评论(0编辑  收藏  举报