第1天:SpringBoot Controller

SpringBoot Controller

  • Web入门
    • 控制器
      • @Controller用法
      • @RestController用法
  • 路由映射
  • 参数传递
  • 数据响应

Web入门

  1. Spring Boot将传统Web开发的mvc、json、tomcat等框架整合,提供了spring-boot-starter-web组件,提供了Web开发场景所需的所有底层依赖,简化了Web应用配置。
  2. 创建Spring Boot项目勾选Spring Web选项之后,会自动将Spring-boot-starter-web组件加入到项目中。
  3. webmvc为Web开发的基础框架,json为JSON数据解析组件,tomcat为自带的容器依赖。
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>

控制器

Spring Boot提供了@Controller和@RestController两种注解来标识此类负责接收和处理HTTP请求。如果请求的是页面和数据,使用@Controller注解即可;如果只是请求数据,则可以使用@RestController注解。

从数据库加载的数据先封装到Model上面,通过控制器绑定到视图上(浏览器页面),最终数据通过控制器到视图(View),View最终显示到浏览器上。控制器主要是接收用户请求和将数据传到浏览器。(用户请求实际上请求的是控制器,控制器去取数据,交给视图,最终视图响应给用户)
第1天:SpringBoot Controller_第1张图片

@Controller用法

【前后端不分离】
请求页面和数据。
以下示例中返回了hello页面和name的数据,在前端页面中可以通过${name}参数获取后台返回的数据并显示。(返回的是一个视图,一个html页面,返回hello会去寻找名为hello.html的页面)
@Controller通常与Thymeleaf模板引擎结合使用。
示例:

@Controller
public class HelloController{
	@RequestMapping("/hello")
	public String index(ModelMap map){
		map.addAttribue(attributeName:"name",attribute:"zhangsan");
		return "hello";
}

@RestController用法

【前后端分离】
采用前后端分离开发的模式,常用@RestController注解,默认情况下,@RestController注解会将返回的对象数据转换为JSON格式。
示例:

@RestController
public class FullStackDemoController {
    @GetMapping("/hello")
    public String hello(){
        return "Hello!";
    }
}

路由映射

控制器如何接收前端请求,主要靠路由映射。

  1. @RequestMapping注解主要负责URL的路由映射,可以添加在Controller类或者具体的方法上。如果添加在Controller类上,则这个Controller中的所有路由映射都会加上此映射规则,如果添加在方法上,则只对当前方法生效。
  2. @RequestMapping注解包含很多属性参数来定义HTTP的请求映射规则。常用的属性参数如下:(前两个常用,后面几个为可选)
    value:请求URL的路径,支持URL模板、正则表达式
    method:HTTP请求方法
    consumes:请求的媒体类型(Content-Type),如application/json
    produces:响应的媒体类型
    params,headers:请求的参数及请求头的值
  3. @RequestMapping注解的通配符匹配非常简单实用,支持“*”【匹配任意字符】,“?”【匹配单个字符】,“**”【匹配任意路径】等通配符。有通配符的优先级低于没有通配符的,比如/user/a.json比/user/*.json优先匹配。有“**”通配符的优先级低于有“*”通配符的。
  4. Method匹配。HTTP请求Method有GET、POST、PUT、DELETE等方式,@RequestMapping注解提供了RequestMethod.GET、RequestMethod.POST、RequestMethod.DELETE、RequestMethod.PUT,分贝对应HTTP请求的Method。【Method匹配也可以使用@GetMapping、@POSTMapping等注解代替】
    代码示例:
@RequestMapping(value = "/getData",method = RequestMethod.GET)
    public String getData(){
        return "hello";
    }

参数传递

数据响应

GET请求

	@RequestMapping(value = "/getTest1",method = RequestMethod.GET)
    public String getTest1(){
        return "GET请求";
    }

    @RequestMapping(value = "/getTest2",method = RequestMethod.GET)
//    http://localhost:8004/getTest2?nickname=zhangsan&phone=123
//    参数可以默认不传递,为空
    public String getTest2(String nickname,String phone){
        System.out.println("nickname:"+nickname);
        System.out.println("phone:"+phone);
        return "GET请求";
    }

    @RequestMapping(value = "/getTest3",method = RequestMethod.GET)
//    http://localhost:8004/getTest3?nickname=zhangsan
//    @RequestParam注解做参数映射,将name映射为nickname,如果不加required = false,则必须传参数nickname
    public String getTest3(@RequestParam(value = "nickname",required = false) String name){
        System.out.println("nickname:"+name);
        return "GET请求";
    }

	@GetMapping("/test/**")
//    @GetMapping("/test/*")
//    *,一级
//    **,任意多级
    public String test(){
        return "通配符请求";
    }

POST请求

	@RequestMapping(value = "/postTest1",method = RequestMethod.POST)
    public String postTest1(){
        return "POST请求";
    }

    @RequestMapping(value = "/postTest2",method = RequestMethod.POST)
    public String postTest2(String username,String password){
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        return "POST请求";
    }

    @RequestMapping(value = "/postTest3",method = RequestMethod.POST)
//    当参数个数较多时,采用实体类User,包含该实体的所有参数
    public String postTest3(User user){
        System.out.println(user);
        return "POST请求";
    }

    @RequestMapping(value = "/postTest4",method = RequestMethod.POST)
//    接收JSON格式的数据需要加一个注解@RequestBody
    public String postTest4(@RequestBody User user){
        System.out.println(user);
        return "POST请求";
    }

User类(POST请求中用到的User类)

public class User {
//    保证前台传的参数名称与其一致
    private String username;
    private String password;

    public String getUsername(){
        return username;
    }

    public void setUsername(String username){
        this.username=username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String toString(){
//        字符串双引号,字符单引号
        return "User{"+
                "username='"+username+'\''+','+
                "password='"+password+'\''+
                '}';
    }
}

你可能感兴趣的:(spring,boot,java,spring)