Spring Boot 学习(三)Controller的使用

四、Controller的使用

4.1 Controller注解简介

    @Controller:处理http请求

    @RestController:Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller

    @RequestMapping:配置url映射

4.2 使用注解@Controller

4.2.1 使用@Controller替换之前的@RestController

代码接上一章,之修改注解部分:

@Controller
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

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

效果:


说明:

    由之前的@RestController改为@Controller之后,访问异常了,下面就介绍几种@Controller注解使用的正确方式。

4.2.2 添加@ResponseBody

代码:

@Controller
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

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

效果:


这个方法也验证了4.1中对@RestController 的说明。

4.2.3 @Controller配合thymeleaf使用

pom增加内容:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

新建index.html文件


html内容:

    <h1>Hello Spring Boot!</h1>

HelloController修改,注意这里去掉了上面使用的@ResponseBody注解:

@Controller
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

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

运行结果:


视频中老师讲到,目前我们已经有很多项目是前后端分离的,此时就不需要使用模板了,且说明模板的方式比较耗费资源。

4.3 注解@RequestMapping用法

4.3.1 同一个方法可以多url访问

代码修改,这里需要将代码先还原到之前@RestController状态:

@RestController
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

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

效果:


可以同时使用两个方法访问到say方法。

4.3.2 对Contrller使用@RequestMapping注解

代码:

@RestController
@RequestMapping(value = "/hello")
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

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

效果:


4.3.3 @RequestMapping注解中Method参数

代码中有:method=RequestMethod.GET,这里说明此方法使用GET类型获取内容,常用为GET和POST方法,其他还有DELETE、PUT等

4.4 获取url参数

4.4.1 @PathVariable获取url中的数据

代码修改:

@RestController
@RequestMapping(value = "/hello")
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping( value = "/say/{id}",method = RequestMethod.GET)
    public String say(@PathVariable("id") Integer id){
        return "id: " + id;
    }
}

效果:



4.4.2 @RequestParam获取请求参数的值

    传统url访问方式:?id=100,这种方式不用在RequestMapping中配置变量,使用示例如下。

代码:

@RestController
@RequestMapping(value = "/hello")
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping( value = "/say",method = RequestMethod.GET)
    public String say(@RequestParam("id") Integer myID){
        return "id: " + myID;
    }
}

效果:


注意:这里的@RequestParam中的值与参数名称不用一样。

4.4.3 @RequestParam参数使用

代码:

@RestController
@RequestMapping(value = "/hello")
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    @RequestMapping( value = "/say",method = RequestMethod.GET)
    public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myID){
        return "id: " + myID;
    }
}

效果:


说明:

    required:是否必传

    defaultValue:默认值

4.4.3 @GetMapping组合注解

    简化@RequestMapping注解,示例代码如下:

@RestController
@RequestMapping(value = "/hello")
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    //@RequestMapping( value = "/say",method = RequestMethod.GET)
    @GetMapping(value = "/say")
    //@PostMapping(value = "/say")
    public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myID){
        return "id: " + myID;
    }
}

这里@GetMapping(value="/say")就是@RequestMapping(value="/say",method=RequestMethod.GET)的简写,同时还有@PostMapping、@PutMapping等。


上一章:Spring Boot 学习(二)项目属性配置

下一章:Spring Boot 学习(四)数据库操作


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值