Spring MVC 的常用注解

@RequestMapping 和 @RestController注解


上面两个注解,是Spring MCV最常用的注解。

@RequestMapping , 他是用来注册接口的路由映射。

路由映射:当一个用户访问url时,将用户的请求对应到某个方法或类的过程叫做路由映射。

@RequestMapping 注解的使用:它既可以又是类,也可以修饰方法,访问的地址是类的路径和方法路径。


但是在这里我们只通过@RequestMapping 是不足以访问网页的,还要通过@RestController注解。

@RestController,他包括了@Controller注解和@ResponseController注解。

@Controller注解返回的是视图 , @ResponseController返回的是各种格式的数据。


在这里我写简单的项目,来掩饰一下,怎么使用的。

如代码:

package com.example.springbootdemo1;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @RequestMapping("/u1")
    public String sayHi(){
        return "hello,Spring 111";
    }

    @RequestMapping("/u2")
    public String hello(){
        return "Hello Spring MVC";
    }

}

我们访问我们哪个输入就访问就行了,根据url的不同,直接访问。

如图访问结果:


@RequestParam注解


使用@RequestParam注解可以对后端的参数重命名,其实就是可以把前端的参数映射到后端来,进而可以对后端的参数,改成自己想要的。


这里写了一个计算器功能的网页,结合前端代码来看一下

如前端后端代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="calc/sum" method="post">
    <h1>计算器</h1>
    数字1:<input name="sum1" type="text"><br>
    数字2:<input name="sum2" type="text"><br>
    <input type="submit" value=" 点击相加 ">
</form>
</body>
</html>
package com.example.springbootdemo1;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/calc")
@RestController
public class CalcController {
    @RequestMapping("/sum")
    public String sum(@RequestParam(value = "sum1" , required = false) Integer array , Integer sum2){
        Integer sum = array + sum2;
        System.out.println(sum);
        return "计算结果:" + sum;
    }
}

我们可以发现,sum1这个参数,我们改成了array,注解里的false表示这里不是必传的参数,默认为true。

如执行结果:

可以看到代码执行没有错误。


@RequestBodoy注解


在日常的开发中,@RequestBody注解主要就是用来传递JOSN格式的数据。

如代码:

@RestController  
@RequestMapping("/users")  
public class UserController {  
  
    @PostMapping("/create")  
    public ResponseEntity<User> createUser(@RequestBody User user) {  
        // 在这里可以使用user对象的属性进行相应的业务逻辑处理  
        // 例如保存用户信息到数据库  
        return ResponseEntity.ok(userService.save(user));  
    }  
}

上面是最常用的注解,下面简单介绍几个,用的还行的。

@PathVariable,这个注解和字面的意思一样,绑定请求url的地址。

如代码:

@RequestMapping("/m8/{id}/{name}")
public String method8(@PathVariable Integer id, @PathVariable("name") String 
userName){
 return "解析参数id:"+id+",name:"+userName;
}

@GetMapping和PostMapping:这两个注解,和我们看到的一样,直接确定了请求的类型,是get还是post。这里就不展示代码了,例在方法上直接写就行。

@RequestPart:用于上传文件。

如代码:

public String getfile(@RequestPart("file") MultipartFile file) throwsIOException {
 //获取⽂件名称
 String fileName = file.getOriginalFilename();
 //⽂件上传到指定路径
 file.transferTo(new File("D:/temp/" + file.getOriginalFilename()));
return "接收到⽂件名称为: "+fileName;
}
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值