SpringBoot(三)_controller的使用

简介: 针对controller 中 如何使用注解进行解析@RestController返回数据类型为 Json 字符串,特别适合我们给其他系统提供接口时使用。@RequestMapping(1) 不同前缀访问同一个方法,此时访问hello和hi 都可以访问到say()这个方法 @R...

针对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)
学习不是要么0分,要么100分的。80分是收获;60分是收获;20分也是收获。有收获最重要。但是因为着眼于自己的不完美,最终放弃了,那就是彻底的0分了。
相关文章
|
6月前
|
XML JSON Java
【SpringBoot】SpringBoot常用注解
【SpringBoot】SpringBoot常用注解
43 0
|
9月前
|
Java
springboot中的注解demo
springboot中的注解demo
|
10月前
|
XML 运维 Java
springboot实用配置
(一)打包与运行 SpringBoot项目快速启动(Linux版) 基于Linux (CenteroS7) 安装JDK,且版本不低于打包时使用的JDK版本 安装包保存在/usr/local/自定义目录中或$HOME下 其他操作参照windows版进行
|
11月前
|
Java 测试技术 Maven
SpringBoot的常见配置
SpringBoot的常见配置
|
11月前
|
XML 开发框架 Java
SpringBoot--常见注解
SpringBoot--常见注解
54 1
|
XML JSON 前端开发
SpringBoot---一些SpringBoot基本注解
SpringBoot---一些SpringBoot基本注解
|
Java Spring
深入理解SpringBoot(三)—— 配置
本文讲述了如何配置SpringBoot的配置文件
200 0
|
JSON Java 数据格式
SpringBoot:SpringBoot2.X常用的注解
SpringBoot:SpringBoot2.X常用的注解
|
XML Java 数据格式
SpringBoot——SpringBoot中使用Servlet的两种方式
SpringBoot——SpringBoot中使用Servlet的两种方式
SpringBoot——SpringBoot中使用Servlet的两种方式
|
JSON Java 数据格式
SpringBoot——SpringBoot中使用拦截器
SpringBoot——SpringBoot中使用拦截器
SpringBoot——SpringBoot中使用拦截器