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

推荐订阅源

G
Google Developers Blog
D
Docker
Stack Overflow Blog
Stack Overflow Blog
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
H
Help Net Security
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
博客园 - 司徒正美
C
Check Point Blog
B
Blog
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
F
Fortinet All Blogs
美团技术团队
D
DataBreaches.Net

博客园 - 追风浪子

构建第一个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")