Spring boot中Controller的使用
一、请求及路径映射部分注解介绍
注解名称 | 描述 |
@Controller | 处理 http 请求 |
@RestController | Spring4 之后新加的注解,原来返回 json,需要 @ResponseBody 配合 @Controller |
@RequestMapping | 配置 url 映射 |
1、@Controller 的使用(了解)
相当于 serverlet 的 jsp,前后端不分离的开发,就模板而言很好性能,所以提倡前后端分离式的开发,这里我就不啰嗦了。
接着再来演示下,这个注解的使用,必须配合模板来使用,首先在 pom 文件中添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
说明:这里的 spring boot 版本必须为 1.4.1,否则使用模板时,不显示页面,报错 404
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
在 src/main/resources/templates/ 下,
创建一个名为 index 的 html 文件,写入内容如下:
<h1>hello,Spring boot!</h1>
编写 Controller,示例如下:
package com.rongrong.springboot.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* @author rongrong
* @version 1.0
* @description:
* @date 2019/12/28 21:32
*/
@Controller
public class HtmlController {
@RequestMapping(value = "/say",method = RequestMethod.GET)public String index(){
return "index";
}
}
启动项目,访问页面:http://localhost:8888/say,显示结果如下:
2、@RestController 的使用
@RestController 在实际开发中应用广泛,与 @controller 相比,不依赖于模板,前后端分离式的开发,开发效率也很高。
3、@RequestMapping 的使用
该注解既可以在类上方使用,又可以方法上使用,在类上方使用,如在该处添加,接口访问时采取路径拼接方式访问, 如:localhost:8888/spring/hellow
示例代码如下:
package com.rongrong.springboot.demo;
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;
/**
* @author rongrong
* @version 1.0
* @description:
* @date 2019/12/26 20:34
*/
@RestController
/**
* 在类上方使用,如在该处添加,接口访问时采取路径拼接方式访问
* 如:localhost:8888/spring/hellow
*/
@RequestMapping("/spring")
public class HellowController {
@Autowired
Person person;
// 在方法 上方使用
@RequestMapping(value = "/hellow",method = RequestMethod.GET)public String say(){
return person.getGName();}
}
访问接口地址:http://localhost:8888/spring/hellow,具体效果如下:
二、请求参数及组合注解介绍
注解名称 | 描述 |
@PathVariable | 获取 url 中的数据 |
@RequestParam | 获取请求参数的值 |
@GetMapping | 组合注解 |
1、@PathVariable 的使用
一般在 restful 风格接口使用居多,形式为:/ 路径 /{变量}
具体示例如下:
//Restful 风格接口
@RequestMapping(value = "/getID/{id}", method = RequestMethod.GET)
public String getID(@PathVariable("id")Integer id) {
return "Id:"+id;
}
访问示例:http://localhost:8888/spring/getID/1,效果如下:
2、@RequestParam 使用
为传统风格形式使用,具体示例如下:
/***
* 传统方式接口
* @RequestParam(value = "id",required = false,defaultValue = "0")
* value 为传入字段名,required 为true时必须传参,defaultValue 为传入参数默认值
* @param id
* @return
*/
@RequestMapping(value = "/getMyId", method = RequestMethod.GET)
public String getMyId(@RequestParam(value = "id",required = false,defaultValue = "0")Integer id) {
return "getMyId: \t"+id;
}
访问示例:http://localhost:8888/spring/getMyId?id=1,效果如下:
3、组合注解及多个路径可以访问同一个接口
@GetMapping 可以理解为 @RequestMapping,具体示例如下:
/**
* 组合注解及多个路径可以访问同一个接口
* @param id
* @return
*/
@GetMapping({"/getUserId","/sayhi"})
//@RequestMapping(value = "/getMyId", method = RequestMethod.GET)
public String getUserId(@RequestParam(value = "id",required = false,defaultValue = "0")Integer id) {
return "getMyId: \t"+id;
}
访问示例:
http://localhost:8888/spring/getUserId?id=1,
http://localhost:8888/spring/sayhi?id=1
效果如下:
__EOF__