Springboot接收参数

接收参数有三个方法。

1、接收id的方法:

@RestController
public class ControllerTest {

    //在这里读取配置文件
    @Autowired
    private Testconfig testconfig;
    //访问路径:http://localhost:8080/hello/5
    @GetMapping(value = "/hello/{id}")
    public String hello(@PathVariable("id") Integer id){
        return "ID:" + id;
    }
}

2、接收参数:

@RestController
public class ControllerTest {

    //在这里读取配置文件
    @Autowired
    private Testconfig testconfig;
    //访问路径:http://localhost:8080/hello?id=1551
    @GetMapping(value = "/hello")
    public String hello(@RequestParam("id") Integer id){
        return "ID:" + id;
    }
}

也可以这样设置,当不传输参数的时候,默认为5:

    //访问路径:http://localhost:8080/hello?id=1551
    @GetMapping(value = "/hello")
    public String hello(@RequestParam(value = "id", required = false, defaultValue = "5") Integer id){
        return "ID:" + id;
    }

原文地址:https://www.cnblogs.com/suiyisuixing/p/8485647.html

时间: 2024-08-30 06:08:20

Springboot接收参数的相关文章

springboot 启动接收参数

springboot 启动可以通过 java -jar xxx.jar 参数 来传递启动参数到程序中.通过args[] 来传递参数非常不优雅,不同系统会有奇异,windows系统会在参数里面自己写入一行标识,Linux系统就不会,传几个参数就会按照数组位数排列. 下面介绍一种优雅的方式,就是借助于application.properties里面的属性,通过@Value将属性注入进来. 代码如下:br/>@Componentpublic class ScriptPath { @Value("

SpringBoot接收对象中包含时间参数,格式化接收时间

SpringBoot接收对象中包含时间参数,前端传过来的时间数据为'2019-12-19 08:08:08',SpringBoot不能自己转化时间为Date,会报错. 解决办法: 使用@JsonFormat注解 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 原文地址:https://www.cnblogs.com/mxh-java/p/12070539.html

SpringBoot(三):springboot启动参数

springboot默认启动入口函数是支持接收参数,并且在整个应用程序内部也可以获取到这些参数,并且如果传递的参数是一些内部定义的参数将会被映射到springboot内部配置项,从而达到配置效果. springboot入口参数传递与获取: 方式1)springboot 配置项目启动传递参数: a)在idea导航Run->Edit Configuration... b)Edit Configuration...下设置启动参数: c)修改SpringBoot启动入口函数: package app;

Struts2中Action接收参数的方法主要有以下三种:

Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数(最原始的方式):     a.定义:在Action类中定义属性,创建get和set方法:     b.接收:通过属性接收参数,如:userName:     c.发送:使用属性名传递参数,如:user1!add?userName=jim: 2.使用DomainModel接收参数:     a.定义:定义Model类,在Action中定义Model类的对象(不需要new),创建该对象的get和set方法

Spring MVC 之请求处理方法可接收参数(二)

请求处理方法可接收参数 今天学习了前三个方法. 1.作用域对象2.单个表单提交数据3.表单数据封装的Bean对象 首先创建一个实体对象. 1 package com.cy.springannotation.entity; 2 /** 3 * 定义一个表单实体类 4 * @author acer 5 * 6 */ 7 public class UserBean { 8 //要求属性名必须要和表单的参数名一样的! 9 private String username; 10 private Strin

Request 接收参数乱码原理解析二:浏览器端编码原理

上一篇<Request 接收参数乱码原理解析一:服务器端解码原理>,分析了服务器端解码的过程,那么浏览器是根据什么编码的呢? 1. 浏览器解码 浏览器根据服务器页面响应Header中的“Content-Type: text/html; charset=gb2312”解码.修改web.config中“responseEncoding=utf-8”,发现服务器页面响应Header变成了“Content-Type: text/html; charset=utf8”. <system.web&g

处理request接收参数的中文乱码的问题:

? POST的解决方案: * POST的参数在请求体中,直接到达后台的Servlet.数据封装到Servlet中的request中.request也有一个缓冲区.request的缓冲区也是ISO-8859-1编码. * 设置request的缓冲区的编码: * request.setCharacterEncoding("UTF-8");  --- 一定要在接收参数之前设置编码就OK. ? GET的解决方案: * 1.修改tomcat的字符集的编码.(不推荐) * 2.使用URLEncod

Struts2中Action接收参数的方法

详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt112 Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数:     a.定义:在Action类中定义属性,创建get和set方法:     b.接收:通过属性接收参数,如:userName:     c.发送:使用属性名传递参数,如:user1!add?userName=Magci: 2.使用DomainModel接收参数:   

Struts2 DomainModel、ModelDriven接收参数

一.DomainModel(域模型) 1. 应用场景:一般我们在struts2的action中接收参数通常是如下方式 package cn.orlion.user; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ private String username; private String password; public String getUsernam