惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
T
Tenable Blog
D
DataBreaches.Net
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
V
Visual Studio Blog
B
Blog
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
InfoQ
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
The Cloudflare Blog
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
Martin Fowler
Martin Fowler
SecWiki News
SecWiki News
T
Threat Research - Cisco Blogs
J
Java Code Geeks
Cyberwarzone
Cyberwarzone
Forbes - Security
Forbes - Security
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
O
OpenAI News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Schneier on Security
Schneier on Security
S
Security @ Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
H
Help Net Security
Y
Y Combinator Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tor Project blog
量子位
U
Unit 42
S
SegmentFault 最新的问题
V
V2EX
D
Docker

博客园 - 追风浪子

构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九) linux修改系统时间为北京时间(CentOS) 构建第一个Spring Boot2.0应用之application.properties和application.yml(八) 构建第一个Spring Boot2.0应用之集成mybatis、Druid(七) 构建第一个Spring Boot2.0应用之集成mybatis(六) 构建第一个Spring Boot2.0应用之Controller(三) 构建第一个spring boot2.0应用之项目启动运行的几种方式(二) 构建第一个Spring Boot2.0应用之项目创建(一) C++ 强制类型转换(转载) 【Android】ContentProvider android图片缩放平移 Android实现程序前后台切换效果 android 调试卡在:Waiting for Debugger - Application XXX is waiting for the debugger to Attach" 解决方法 将DataTable 数据插入 SQL SERVER 数据库 android adb shell 命令大全 三种方法实现Javascript控制ScrollBar(滚动条) 从零开始学习jQuery之你必须知道的JavaScript 快速判断JavaScript对象是否存在的十个方法 jQuery插件
构建第一个Spring Boot2.0应用之RequestMapping(四)
追风浪子 · 2018-05-20 · via 博客园 - 追风浪子

   在学习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")