构建第一个Spring Boot2.0应用之RequestMapping(四)
在学习controller的时候,测试了在RequestMapping中,value参数中配置集合,实现不同的URL访问同一方法。
本章继续学习和测试RequestMapping的其他特性。
一.PathVariabe获取URL参数,代码如下:
package com.yy.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; /**
* Created by Administrator on 2018-05-18.
*/
@RestController
//@Controller
public class HelloController {
@RequestMapping(value = "/hello1",method= RequestMethod.GET)
public String sayHello1()
{
String hello="index";
return hello;
}
@ResponseBody
@RequestMapping(value = {"/hello2","/index2"},method= RequestMethod.GET)
public String sayHello2()
{
String hello="index"; return hello;
} @RequestMapping(value = "/hello3",method= RequestMethod.GET)
public ModelAndView sayHello3()
{
ModelAndView mv = new ModelAndView("index");
return mv;
}
// URL参数
@RequestMapping(value = "/hello4/{name}",method= RequestMethod.GET)
public String sayHello4(@PathVariable("name") String name )
{
String str=name;
return str;
} }
启动应用,访问URL:http://localhost:8088/sptest/hello4/lilei,则方法sayHello4获取的name参数值为lilei,方法返回name值,则显示如下:

二.使用@RequestMapping给整个类指定URL映射
package com.yy.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; /**
* Created by Administrator on 2018-05-18.
*/
@RestController
//@Controller
@RequestMapping(value="my")
public class HelloController {
@RequestMapping(value = "/hello1",method= RequestMethod.GET)
public String sayHello1()
{
String hello="index";
return hello;
}
@ResponseBody
@RequestMapping(value = {"/hello2","/index2"},method= RequestMethod.GET)
public String sayHello2()
{
String hello="index"; return hello;
} @RequestMapping(value = "/hello3",method= RequestMethod.GET)
public ModelAndView sayHello3()
{
ModelAndView mv = new ModelAndView("index");
return mv;
}
// URL参数
@RequestMapping(value = "/hello4/{name}",method= RequestMethod.GET)
public String sayHello4(@PathVariable("name") String name )
{
String str=name;
return str;
} }
其他不变,给HelloController类添加@RequestMapping注解。启动应用访问URL:

三.按照传统方式Url+"?name=lilei"传递参数
//传统URL参数
@RequestMapping(value = "/hello5",method= RequestMethod.GET)
public String sayHello5(@RequestParam(value="name",required = false,defaultValue = "james") String name )
{
String str=name;
return str;
}
获取参数使用@RequestParam,其中value值为url中参数的名称;required默认为true,表示参数必须,此处修改为false;defaultValue为该参数默认值,如果url中没有为该参数指定值,则使用该默认值,如果参数已传递,则用获取的参数值覆盖该默认值。
用如下URL访问:http://localhost:8088/sptest/my/hello5/?name=chole,显示如下:

四.使用GetMapping代替RequestMapping
其他不变,将上面代码中的sayHello5注解改为@GetMapping(value = "/hello5")

构建第一个Spring Boot2.0应用之RequestMapping(四)的更多相关文章
- 构建第一个Spring Boot2.0应用之项目创建(一)
1.开发环境 IDE: JAVA环境: Tomcat: 2.使用Idea生成spring boot项目 以下是使用Idea生成基本的spring boot的步骤. (1)创建工程第一步 (2)创建工 ...
- 构建第一个Spring Boot2.0应用之集成mybatis(六)
一.环境: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE 二.步骤 方式一:利用配置文件配 ...
- 构建第一个Spring Boot2.0应用之application.properties和application.yml(八)
本节学习在项目中配置文件配置的方式,一种是通过applicaiton.properties,一种是通过application.yml方式. 一.环境: IDE:IntelliJ IDEA 2017.1 ...
- 构建第一个Spring Boot2.0应用之集成mybatis、Druid(七)
一.环境: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE 二.说明: 本文综合之 ...
- 构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九)
一.环境: Windows: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE Linux(C ...
- 构建第一个spring boot2.0应用之项目启动运行的几种方式(二)
方法一. 配置Run/Debug Configuration 选择Main Class为项目 Application启动类(入口main方法) (2).进行项目目录,即包含pom.xml的目录下,启 ...
- 构建第一个Spring Boot2.0应用之Controller(三)
Controller控制器主要是接收浏览器请求.下面说一说@Controller注解和@RestController的区别: (1)@Controller类中的方法可以直接通过返回String跳转到j ...
- 快速搭建spring boot2.0 项目
快速搭建spring boot2.0+mybatis+thymeleaf 项目 使用工具STS 3.9.7(eclipse) 首先创建一个spring boot2.0项目(具体创建方法就不写了) 然后 ...
- spring boot2.0(一 ) 基础环境搭建
1.基础配置 开发环境:window jdk版本:1.8(spring boot2.0最低要求1.8) 开发工具:eclipse 构建方式:maven3 2.POM配置文件 <project x ...
随机推荐
- java连接sqlserver2005数据库
java连接sqlserver2005数据库 首先得下载驱动程序到微软网站下载Microsoft JDBC Driver 4.0 for SQL Server 下载地址 :http://msdn. ...
- sklearn交叉验证法(Cross Validation)
import numpy as np from sklearn import datasets from sklearn.cross_validation import train_test_spli ...
- AT指令集
通用指令 at+cala 设置警报日期和时间 at+cgmi 厂家认证请求,返回模块厂家信 at+cgmm 模式认证请求,返回模块使用频段 at+cgmr 修正认证请求,返回软件版本 at+cg ...
- 阶段2-新手上路\项目-移动物体监控系统\Sprint3-移动监控主系统设计与开发
移动图像监控系统 去找一些相关开源程序进行移植:百度搜索-linux 移动监控 motion是一套免费开源的移动图像监测程序 前面我们已经使用了很多开源软件,他们的使用方法都是大同小异的 1).先在当 ...
- Flash builder发布Air程序时设备配置文件supportedProfiles的配置
1. 发布的程序:需要访问本地进程,那么只能发布为exe程序才可以. 此时supportedProfiles 配置为 extendedDesktop desktop desktop保证能发布a ...
- Good Bye 2014 B. New Year Permutation(floyd )
题目链接 题意:给n个数,要求这n个数字小的尽量放到前面,求一个最小的. 给一个矩阵s[i][j]==1,表示位置 i 的数字可以和 位置 j 的数字交换. 分析: 刚开始用的是3个循环,每次都找一个 ...
- 微信JSApi支付---常见问题
1.支付一直报 “get_brand_wcpay_request:false” 错误 原因: 商户平台上设置的[支付授权目录]路劲不正确,比如:支付的页面的域名是:www.xxx.com/pay/s ...
- Educational Codeforces Round 53C(二分,思维|构造)
#include<bits/stdc++.h>using namespace std;const int N=1e6+6;int x[N],y[N];int sx,sy,n;char s[ ...
- go培训课程都学什么?go语言框架学习:xorm框架知识介绍
xorm框架介绍 xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便. 通过xorm框架,开发者可以方便的使用各种封装好的方法来代替原生的sql语句.这样就降低了我们开发者使 ...
- MySQL主从配置实战笔记
其实网上已经有关于MySQL主从复制的很丰富全面的资料了,这里写点东西主要是为了给自己加深印象. 复制原理 MySQL主从复制是内建的非常强大的功能,主要应用于数据备份,负载均衡等方面.因为配置相对比 ...