@Controller
单独使用@Controller用于返回一个视图操作。Spring MVC中@Controller中的方法可以直接返回模板名称接下来 Thymeleaf 模板引擎会自动进行渲染,模板中的表达式支持Spring表达式语言(Spring EL)。
@Controller
public class HelloController {
@GetMapping("/hello")
public String greeting(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello";
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@RestController
@RestController(Spring4+)相当于@Controller+@ResponseBody,返回json或者xml格式数据。
@ResponseBody 注解的作用是将 Controller 的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到HTTP 响应(Response)对象的 body 中,通常用来返回 JSON 或者 XML 数据,返回 JSON 数据的情况比较多。
@RequestParam
凡是放在URL中的参数都可以使用@RequestParam获取,不论是post请求还是get请求。
@RequsetBody
凡是在请求体中的参数都可以使用@RequsetBody获取,但是get请求没有请求体。
@RequsetPart
用于文件类型的参数获取
@RequestMapping("uploadFile")
public JsonResult uploadFile(@RequestPart("file") MultipartFile file, @RequestParam String bucket){
String fileUrl = aliossService.uploadFile(file, bucket);
Map<String,String> result = new HashMap<>();
result.put("fileUrl",fileUrl);
return success(result);
}