欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot入门(三)Controller的使用

程序员文章站 2022-05-29 22:19:34
...

浅析

@Controller:处理http请求
@RestController:Spring4之后新加的注解,用于代替原来@ResponseBody aaa@qq.com的组合
@RequestMapping:配置url映射(用户通过访问某个url访问到我们某个方法)

@Controller

原来的代码

@RestController
public class HelloController {
    @Autowired
    private PersonProperties mPersonProperties;

    @RequestMapping(value="/helloctrl", method=RequestMethod.GET)
    public String say() {
        return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge();
    }
}

使用@Controller(仅为了解。因为后面一般还是使用@RestController)
1、pom.xml中添加spring官方模板

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

2、在src/main/resources/template文件夹下创建index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>
    Hello Controller
</body>
</html>

3、修改@RestController为@Controller,修改返回值为index

@Controller
public class HelloController {
    @Autowired
    private PersonProperties mPersonProperties;

    @RequestMapping(value="/helloctrl", method=RequestMethod.GET)
    public String say() {
        return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge();
    }
}

4、运行结果
SpringBoot入门(三)Controller的使用

@aaa@qq.comaaa@qq.com组合

@Controller
@ResponseBody
public class HelloController {
    @Autowired
    private PersonProperties mPersonProperties;

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

SpringBoot入门(三)Controller的使用

@RequstMapping配置url

SpringBoot入门(三)Controller的使用
访问路径可以是
http://localhost:8088/controller/helloctrl
或者
http://localhost:8088/controller/hi

如何处理url中的参数


@PathVariable:获取url中的数据
@RequestParam:获取请求参数的值
@GetMapping:组合注解(简化@RequestMapping(value= {“/helloctrl”}, method=RequestMethod.GET)aaa@qq.com(value= {“/helloctrl”}))

@PathVariable

@RestController
@RequestMapping("/controller")
public class HelloController {
    @Autowired
    private PersonProperties mPersonProperties;

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

SpringBoot入门(三)Controller的使用
还可以如下写:

@RequestMapping(value= {"/{id}/helloctrl"}, method=RequestMethod.GET)

访问地址

http://localhost:8088/controller/100/helloctrl

设置默认值

@RestController
@RequestMapping("/controller")
public class HelloController {
    @Autowired
    private PersonProperties mPersonProperties;

    @RequestMapping(value= {"/helloctrl"}, method=RequestMethod.GET)
    //required是否一定要传递值,设置为false,不需要
    public String say(@RequestParam(value="id",required=false,defaultValue="10") Integer myid) {
        return "id"+myid;
    }
}

SpringBoot入门(三)Controller的使用

@GetMapping

@RestController
@RequestMapping("/controller")
public class HelloController {
    @Autowired
    private PersonProperties mPersonProperties;

    @GetMapping(value="/helloctrl")
    public String say(@RequestParam(value="id",required=false,defaultValue="20") Integer myid) {
        return "id"+myid;
    }
}

与@GetMapping相对应的还有@PostMapping、@PutMapping、@DeleteMapping