2小时学会SpringBoot(4-1)

Controller的使用

Controller处理http请求
@RestControllerSpring4之后新加的注解,原来json需要@ResponseBody配合@Controller
@RequestMapping配置url映射

thymeleaf了解一下即可,现在的开发方式都是前后端分离的。
@RestController等同于@Controller和@ResponseBody

如果希望访问 http://127.0.0.1:8080/hihttp://127.0.0.1:8080/hello 都可以访问到 /hello 接口。在这里需要写成集合。

@RequestMapping(value = {"/hello","hi"},method = RequestMethod.GET)

@RequestMapping还有一种使用方式是给整个类使用。

package com.fiona;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

    @Autowired
    private GirlProperties girlProperties;

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

如果要使用除Get以外的其他方式,常用的是Get和Post。改成post请求

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

    @Autowired
    private GirlProperties girlProperties;

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

浏览器访问 http://127.0.0.1:8080/hello/say 报405。使用postman选择post方法进行请求。

不写请求方式,Get和Post均可访问到,但不推荐。为了安全起见。

@RequestMapping(value = "/say")
@PathVariable获取url中的数据
@RequestParam获取请求参数的值
@GetMapping组合注解
@RestController
@RequestMapping("/hello")
public class HelloController {

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

浏览器访问 http://127.0.0.1:8080/hello/say/23
显示 id:23

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

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

浏览器请求 http://127.0.0.1:8080/hello/100/say
显示 id:100

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

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

浏览器请求 http://127.0.0.1:8080/hello/say?id=50
显示 id:50
@RequestParam(“id”)引号里面的名字要和浏览器的变量名一致,但后面但id可以不叫id,取其他名字也可以。

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

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

浏览器请求 http://127.0.0.1:8080/hello/say?id=111
显示 id:111

假如不传id
在这里插入图片描述

在这里插入图片描述

给id设定一个默认值为0

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

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

在这里插入图片描述
在这里插入图片描述

可以使用

@GetMapping(value = "/say")

代替

@RequestMapping(value = "/say",method = RequestMethod.GET)

还有 @PostMapping @PutMapping这样的组合书写。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值