针对controller 中 如何使用注解进行解析

@RestController

  • 返回数据类型为 Json 字符串,特别适合我们给其他系统提供接口时使用。

@RequestMapping

(1) 不同前缀访问同一个方法,此时访问hello和hi 都可以访问到say()这个方法

    @RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
public String say(){
return girlProperties.getName();
}

(2)给类一个RequestMapping, 访问时就是:http://localhost:8099/hello/say

@RestController
@RequestMapping("/hello")
public class HelloController { @Resource
private GirlProperties girlProperties;
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(){
return girlProperties.getName();
}
}

@PathVariable:获取url中的数据

@RestController
@RequestMapping("/hello")
public class HelloController { @Resource
private GirlProperties girlProperties;
@RequestMapping(value = "/say/{id}",method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id){
return "id :"+id;
}
}

访问http://localhost:8099/hello/say/100, 结果如下

id :100

@RequestParam :获取请求参数的值

(1) 正常请求

@RestController
@RequestMapping("/hello")
public class HelloController { @Resource
private GirlProperties girlProperties;
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam("id") Integer id){
return "id :"+id;
}
}

访问 http://localhost:8099/hello/say?id=111 结果如下

id :111

(2)设置参数非必须的,并且设置上默认值

@RestController
@RequestMapping("/hello")
public class HelloController { @Resource
private GirlProperties girlProperties;
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){
return "id :"+id;
}
}

访问http://localhost:8099/hello/say 结果如下

id :0

@GetMapping ,当然也有对应的Post等请求的简化写法

  • 这里对应的就是下面这句代码
 @GetMapping("/say")
//等同于下面代码
@RequestMapping(value = "/say",method = RequestMethod.GET)

SpringBoot(三)_controller的使用的更多相关文章

  1. spring-boot (三) spring data jpa

    学习文章来自:http://www.ityouknow.com/spring-boot.html spring data jpa介绍 首先了解JPA是什么? JPA(Java Persistence ...

  2. SpringBoot 三种方式配置 Druid(包括纯配置文件配置)

    记录一下在项目中用纯 YML(application.yml 或者 application.properties)文件.Java 代码配置 Bean 和注解三种方式配置 Alibaba Druid 用 ...

  3. SpringBoot(三) -- SpringBoot与日志

    一.日志的起源 现在假设一个开发人员在开发一个大型系统,由于这个系统过于庞大没在很多的地方将关键的数据使用System.out.println()打印,但是当我们在项目正式上线时又需要去除,在项目bu ...

  4. SpringBoot(三)SpringBoot自动配置

    我们都知道SpringBoot帮助我们集成了许多组件和配置,那么SpringBoot是如何集成这些配置并在启动是自动进行配置呢.说到这就不得又需要回过头来看一下@SpringBootApplicati ...

  5. Java开发学习(三十六)----SpringBoot三种配置文件解析

    一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...

  6. SpringBoot(三) - Slf4j+logback 日志,异步请求,定时任务

    1.Slf4j+logback 日志 SpringBoot框架的默认日志实现:slf4j + logback: 默认日志级别:info,对应了实际生产环境日志级别: 1.1 日志级别 # 常见的日志框 ...

  7. springboot(三):Spring boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

  8. spring-boot(三) HowTo

    Spring Boot How To 1. 简介 本章节将回答一些常见的"我该怎么做"类型的问题,这些问题在我们使用spring Boot时经常遇到.这绝不是一个详尽的列表,但它覆 ...

  9. SpringBoot三种配置Dubbo的方式

    *必须首先导入dubbo-starter (1).使用SpringBoot配置文件(application.properties或application.yml) dubbo.application. ...

随机推荐

  1. EMC Isilon(OneFS)误删文件数据恢复过程<存储数据恢复>

    [科普Isilon的存储结构] Isilon内部使用的是分布式文件系统OneFS.在Isilon存储集群里面每个节点均为单一OneFS文件系统,所以Isilon在支持横向扩展的同时并不会影响数据正常使 ...

  2. nginx for Windows

    zt from nginx official site. Known issuesPossible future enhancements Version of nginx for Windows u ...

  3. 免费私有gitLab服务推荐

    阿里云code :https://code.aliyun.com/,可以免费开50个私有项目. 配套的持续交付:https://crp.aliyun.com

  4. MySQL 的索引优化

    索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左右性能开始逐渐下降,虽然官方文档说500~800w记录,所以大数据量建立索引是非常有必要的.My ...

  5. 关于前端本地压缩图片,兼容IOS/Android/PC且自动按需加载文件之lrz.bundle.js

    一.介绍说明主要特点: ①在前端压缩好要上传的图片可以更快的发送给后端,因此也特别适合在移动设备上使用. ②兼容IOS/Android,修复了IOS/Android某些版本已知的BUG. ③按需加载文 ...

  6. manifold tangent classifier

    The Manifold Tangent Classifier (MTC) Putting it all together, here is the high level summary of how ...

  7. builder设计模式(摘录ITeye文章lintomny)

    对于Builder模式很简单,但是一直想不明白为什么要这么设计,为什么要向builder要Product而不是向知道建造过程的Director要.刚才google到一篇文章,总算清楚了.在这里转贴一下 ...

  8. FastDFS单机版安装

    FastDFS 分布式文件系统 1 目标 了解项目中使用FastDFS的原因和意义. 掌握FastDFS的架构组成部分,能说出tracker和storage的作用. 了解FastDFS+nginx上传 ...

  9. TensorFlow-谷歌深度学习库 命令行参数

    程序的入口: tf.app.run tf.app.run( main=None, argv=None ) 运行程序,可以提供'main'函数以及函数参数列表.处理flag解析然后执行main函数. 什 ...

  10. MySQL松散索引扫描与紧凑索引扫描

    什么是松散索引? 答:实际上就是当MySQL 完全利用索引扫描来实现GROUP BY 的时候,并不需要扫描所有满足条件的索引键即可完成操作得出结果. 要利用到松散索引扫描实现GROUP BY,需要至少 ...