SpringBoot入门(二):Controller的使用
Controller中注解的使用:
@Controller
●该注解用来响应页面,必须配合模板来使用
@RestController
●该注解可以直接响应字符串,返回的类型为JSON格式
1 package com.example.demo.controller;
2
3 import org.springframework.web.bind.annotation.RequestMapping;
4 import org.springframework.web.bind.annotation.RestController;
5
6 @RestController
7 public class HelloWorldController {
8
9 @RequestMapping("/")
10 public String index() {
11 return "Hello World";
12 }
13 }
启动程序,访问http://localhost:8080/,返回Hello World
@RequestMapping
●指定URL访问中的前缀
1 package com.example.demo;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6 @SpringBootApplication
7 public class DemoApplication {
8
9 public static void main(String[] args) {
10 SpringApplication.run(DemoApplication.class, args);
11 }
12 }
访问http://localhost:8080/hello,返回Hello World
@RequestParam
●获取请求URL中参数的变量
1 package com.example.demo.controller;
2
3 import org.springframework.web.bind.annotation.RequestMapping;
4 import org.springframework.web.bind.annotation.RequestMethod;
5 import org.springframework.web.bind.annotation.RequestParam;
6 import org.springframework.web.bind.annotation.RestController;
7
8 @RestController
9 public class HelloWorldController {
10
11 @RequestMapping(value="/hello",method=RequestMethod.GET)
12 public String index(@RequestParam("name")String name) {
13 return "Hello World:"+name;
14 }
15 }
访问http://localhost:8080/hello?name=张三,返回Hello World:张三
@PathVariable
●获取请求URL中路径的变量
1 package com.example.demo.controller;
2
3 import org.springframework.web.bind.annotation.PathVariable;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestMethod;
6 import org.springframework.web.bind.annotation.RestController;
7
8 @RestController
9 public class HelloWorldController {
10
11 @RequestMapping(value="/hello/{name}",method=RequestMethod.GET)
12 public String index(@PathVariable("name")String name) {
13 return "Hello World:"+name;
14 }
15 }
访问http://localhost:8080/hello/张三,返回Hello World:张三
@GetMapping/@PostMapping
●与RequestMapping类似,不同的是指定了请求的方式(get/post)
1 package com.example.demo.controller;
2
3 import org.springframework.web.bind.annotation.GetMapping;
4 import org.springframework.web.bind.annotation.RestController;
5
6 @RestController
7 public class HelloWorldController {
8
9 @GetMapping(value="/hello")
10 public String index() {
11 return "Hello World";
12 }
13 }
总结
Controller是SpringBoot里比较常用的组件,通过匹配客户端提交的请求,分配不同的处理方法,然后向客户端返回结果。
SpringBoot入门(二):Controller的使用的更多相关文章
- springboot 入门二- 读取配置信息一
在上篇入门中简单介绍下springboot启动使用了大量的默认配置,在实际开发过程中,经常需要启动多个服务,那端口如何手动修改呢? 此篇就是简单介绍相关的配置文件信息. Spring Boot允许外部 ...
- SpringBoot入门 (二) 属性文件读取
在上一篇中介绍了在idea中创建springboot工程及使用web工程输出“helloworld”到前端页面,本文学习在springboot工程中读取属性文件中的属性值. 一 自定义属性 在appl ...
- SpringBoot入门(二):日志及自定义属性
这一章主要说springboot中日志的配置.自定义属性的配置与读取.分环境的yml配置文件(如本地环境.测试环境.生产环境等).比较偏向实际开发,较为实用,前面一章的一些基本创建在这里就不多废话了. ...
- SpringBoot入门二:与Mybatis整合
一.编程步骤 1.引入依赖 springboot相关依赖(略).mybatis-spring-boot-starter.mysql.druid.lombook <dependency> & ...
- SpringBoot入门基础
目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...
- SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...
- SpringBoot入门(二)——起步依赖
本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...
- SpringBoot开发二十-Redis入门以及Spring整合Redis
安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis 的常用命 ...
- SpringBoot入门系列(十二)统一日志收集
前面介绍了Spring Boot 异常处理,不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.html. 今 ...
- 【Java】SpringBoot入门学习及基本使用
SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...
随机推荐
- sql年、季度、月的第一天
SELECT dateadd(yy,datediff(yy,0,getdate()),0) select dateadd(qq,datediff(qq,0,getdate()),0) select d ...
- LoadRunner压力测试(web)
1.打开Virtual User Generator->新建脚本->选择创建新脚本类型,web-HTTP,HTML->创建 2.录制脚本 3.停止脚本录制 4.创建controlle ...
- 2.3Dmax界面_视图调整
一.试图模型显示效果的切换 '默认是真实显示效果' 线框模式 快捷键F3 ----> 真实显示效果和线框显示效果的切换(切换到线框显示效果再按F3就切换到了真实显示效果). 线面模式 快捷键F4 ...
- white-space: pre-line;的坑
html模版解析换行 这是字符串 跟标签设置white-space: pre-line: pre兼容ie8,pre-line不兼容ie 6-7 这行文字开头是没有空格的但是还是有很大的空格,代码方法截 ...
- Springboot ehcache/redis双缓存问题
问题1:两个CacheManager 会报两个相同的实现类错误需要继承CachingConfigurerSupport 重写cacheManager方法,指定默认一个返回缓存提供者 @Configur ...
- apt常用命令 - 搬运
Debian/Ubuntu基础的系统可以使用apt安装.卸载软件包 转自:https://www.jb51.net/os/Ubuntu/56362.html APT 常用命令如下: apt list ...
- LaTeX in 24 Hours - 2. Fonts Selection
文章目录 本章内容:字体 2.1 Text-Mode Fonts 2.2 Math-Mode Fonts 2.3 Emphasized Fonts 2.4 Colored Fonts 其他章节目录: ...
- Python中的startswith()函数用法
函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一.函数说明语法:string.startswith(str, beg=0,end=len(string)) ...
- @Value属性值读取
1.在父类定义属性DQ,并通过配置初始化 @Configuration public class DQConfig { public static String DQ; @Value("${ ...
- 归一化,正则化,标准化,dropout的联系与区别
归一化(Normalization)是指将数据缩放到一定范围内,一般是将数据映射到[0,1]或[-1,1]之间.这样可以避免不同特征的数值差异过大而导致的训练困难,同时也有助于加快梯度下降的收敛速度. ...