become who you want to |

张三Blog

园龄:3年4个月粉丝:3关注:0

SpringBoot和SpringMVC中常用注解详解

1.Spring Web MVC 与 Spring Bean 注解

1.1 Spring Web MVC 注解

1.1.1@RequestMapping

@RequestMapping注解的主要用途是将Web请求与请求处理类中的方法进行映射。Spring MVC和Spring WebFlux都通过RquestMappingHandlerMapping和RequestMappingHndlerAdapter两个类来提供对@RequestMapping注解的支持。
@RequestMapping注解对请求处理类中的请求处理方法进行标注;@RequestMapping注解拥有以下的六个配置属性:

  • value:映射的请求URL或者其别名
  • method:兼容HTTP的方法名
  • params:根据HTTP参数的存在、缺省或值对请求进行过滤
  • header:根据HTTP Header的存在、缺省或值对请求进行过滤
  • consume:设定在HTTP请求正文中允许使用的媒体类型
  • product:在HTTP响应体中允许使用的媒体类型

提示:在使用@RequestMapping之前,请求处理类还需要使用@Controller或@RestController进行标记

下面是使用@RequestMapping的两个示例:

package com.xcc.ecrm.controller;

import com.github.pagehelper.PageInfo;
import com.xcc.ecrm.pojo.User;
import com.xcc.ecrm.service.UserService;
import com.xcc.ecrm.utils.RespBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author ZhangSan_Plus
 * @Date 2022/3/17 15:14
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/api/ecrm")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "users",method = RequestMethod.GET)
    public RespBean users(@RequestParam("page") Integer page) {
        PageInfo<User> info = userService.users(page);
        return RespBean.ok("success", info);
    }
}

1.1.2 @RequestBody

@RequestBody在处理请求方法的参数列表中使用,它可以将请求主体中的参数绑定到一个对象中,请求主体参数是通过HttpMessageConverter传递的,根据请求主体中的参数名与对象的属性名进行匹配并绑定值。此外,还可以通过@Valid注解对请求主体中的参数进行校验。

下面是一个使用@RequestBody的示例:

@RestController
@CrossOrigin
@RequestMapping("/api/ecrm")
public class LoginController {
    @Autowired
    private UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @PostMapping("/login")
    public RespBean login(@RequestBody User user) {
        if (StringUtils.isBlank(user.getUsername()) && StringUtils.isBlank(user.getPassword())) {
            return RespBean.error("用户名或者密码不能为空");
        }
        User registryUser = userService.login(user.getUsername(), user.getPassword());
        if (Objects.isNull(registryUser)) {
            return RespBean.error("用户不存在或者不存在");
        }
        String token = JwtUtils.createToken(registryUser.getUsername(), registryUser.getRoleId().toString(), registryUser.getUsername(), user.getPassword());
        registryUser.setToken(token);
        return registry(registryUser);
    }
}

1.1.3 @GetMapping

@GetMapping注解用于处理HTTP GET请求,并将请求映射到具体的处理方法中。具体来说,@GetMapping是一个组合注解,它相当于是@RequestMapping(method=RequestMethod.GET)的快捷方式。

下面是@GetMapping的一个使用示例:

package com.xcc.ecrm.controller;

import com.github.pagehelper.PageInfo;
import com.xcc.ecrm.pojo.User;
import com.xcc.ecrm.service.UserService;
import com.xcc.ecrm.utils.RespBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author ZhangSan_Plus
 * @Date 2022/3/17 15:14
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/ecrm")
public class UserController {
    @Resource
    private UserService userService;
    @GetMapping("users")
    public RespBean users(@RequestParam("page") Integer page) {
        PageInfo<User> info = userService.users(page);
        return RespBean.ok("success", info);
    }
}

1.1.4 @PostMapping

@PostMapping注解用于处理HTTP POST请求,并将请求映射到具体的处理方法中。@PostMapping与@GetMapping一样,也是一个组合注解,它相当于是@RequestMapping(method=HttpMethod.POST)的快捷方式。

下面是使用@PostMapping的一个示例:

package com.xcc.ecrm.controller;

import com.xcc.ecrm.pojo.CorpSystemInfo;
import com.xcc.ecrm.service.CorpSystemInfoService;
import com.xcc.ecrm.utils.RespBean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ClassName CorpSystemInfoController
 * @Description TODO
 * @Author ZhangSan_Plus
 * @Date 2022/3/15 19:11
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/ecrm")
public class CorpSystemInfoController {
    @Resource
    private CorpSystemInfoService corpSystemInfoService;

    @PostMapping("corp/updateInfo")
    public RespBean updateInfo(@RequestBody CorpSystemInfo corpSystemInfo) {
        boolean flag = corpSystemInfoService.updateInfo(corpSystemInfo);
        if (flag) {
            return RespBean.ok("success");
        } else {
            return RespBean.error("fail");
        }
    }
}

1.1.5 @PutMapping

@PutMapping注解用于处理HTTP PUT请求,并将请求映射到具体的处理方法中,@PutMapping是一个组合注解,相当于是@RequestMapping(method=HttpMethod.PUT)的快捷方式。

下面是使用@PutMapping的一个示例:

package com.xcc.ecrm.controller;

import com.xcc.ecrm.pojo.CorpSystemInfo;
import com.xcc.ecrm.service.CorpSystemInfoService;
import com.xcc.ecrm.utils.RespBean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ClassName CorpSystemInfoController
 * @Description TODO
 * @Author ZhangSan_Plus
 * @Date 2022/3/15 19:11
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/ecrm")
public class CorpSystemInfoController {
    @Resource
    private CorpSystemInfoService corpSystemInfoService;
	@PutMapping("corp/update")
    public RespBean update(@RequestBody CorpSystemInfo corpSystemInfo){
        boolean flag = corpSystemInfoService.updateInfo(corpSystemInfo);
        if (flag) {
            return RespBean.ok("success");
        } else {
            return RespBean.error("fail");
        }
    }
}

1.1.6 @DeleteMapping

@DeleteMapping注解用于处理HTTP DELETE请求,并将请求映射到删除方法中。@DeleteMapping是一个组合注解,它相当于是@RequestMapping(method=HttpMethod.DELETE)的快捷方式。

下面是使用@DeleteMapping的一个示例:

package com.xcc.ecrm.controller;

import com.xcc.ecrm.pojo.CorpSystemInfo;
import com.xcc.ecrm.service.CorpSystemInfoService;
import com.xcc.ecrm.utils.RespBean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ClassName CorpSystemInfoController
 * @Description TODO
 * @Author ZhangSan_Plus
 * @Date 2022/3/15 19:11
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/ecrm")
public class CorpSystemInfoController {
    @Resource
    private CorpSystemInfoService corpSystemInfoService;
	@DeleteMapping("corp/delete/{id}")
    public RespBean delete(@PathVariable(name="id")String id){
        boolean flag = corpSystemInfoService.delete(id);
        if (flag) {
            return RespBean.ok("success");
        } else {
            return RespBean.error("fail");
        }
    }
 }

1.1.7 @PatchMapping

@PatchMapping注解用于处理HTTP PATCH请求,并将请求映射到对应的处理方法中。@PatchMapping相当于是@RequestMapping(method=HttpMethod.PATCH)的快捷方式。

下面是一个简单的示例:

本文作者:张三Blog

本文链接:https://www.cnblogs.com/zhangsan-plus/p/16503248.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   张三Blog  阅读(55)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起