SpringBoot常用注解

  • @Autowired

将bean自动注入到类的属性中。该注解可以直接标注在属性上,也可以标注在构造器,方法,甚至是传入参数上,其实质都是调用了setter方法注入。

  • @ComponentScan

用来自动扫描被这些注解标识的类,最终生成ioc容器里的bean,默认扫描范围是@ComponentScan注解所在配置类包及子包的类

  • @SpringBootConfiguration

与@Configuration作用相同,都是用来声明当前类是一个配置类,这里表明是springboot主类使用的配置类

  • @EnableAutoConfiguration

是springboot实现自动化配置的核心注解,通过这个注解把spring应用所需的bean注入容器中

  • @Repository

持久层(dao)注入spring容器

  • @Service

业务逻辑层(server)注入spring容器

  • @Controller

控制层(controller)注入spring容器

  • @Component

普通pojo注入spring容器

  • @ResponseBody

表示该方法的返回结果直接写入 HTTP response body 中,而不会被解析为跳转路径,即不会经过视图解析器,返回什么数据即在页面输入什么数据

  • RequestMapping

提供路由信息,负责URL到Controller中的具体函数的映射。通过该注解就可以通过配置的url进行访问,方式可以是get或post请求,两种方式均可

  • RestController

用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集

  • @GetMapping

限定了只能是Get请求

  • @PostMapping

限定了只能是Post请求

  • @Value

用于获取bean的属性,一般用于读取配置文件的数据或赋值,作用在变量上

//读取配置文件数据
@Value("${msg}");
private String msg;

//直接赋值
@Value("Hello World");
private String str;
  • @ConfigurationProperties

注入Bean属性,然后再通过当前Bean获取注入值,作用在类上

  • @PropertySource

指定要读取的配置文件,可以和@Value或@ConfigurationProperties配合使用

  • @Configuration

作用于类上面,表明这是一个配置类

  • @Bean

产生一个Bean对象加入Spring IOC容器

  • @RequestParam

获取查询参数。url?name=这种形式

  • @PathVariable

获取路径参数。url/{id}这种形式

  • @RequestParam

获取Body的参数,一般用于post获取参数

@GetMapping("/requestParam")
    @ResponseBody
    public Map<String, String> requestParam(
            UserDto userDto,//通过一个实体类来接收,字段名必须一致
            @RequestParam(value = "id", required = false) String userId,
            @RequestParam(value = "name", required = false) String userName,
            @RequestParam(value = "pageIndex", required = true, defaultValue = "1") String pageIndex,
            @RequestParam(value = "pageSize", required = true, defaultValue = "5") String pageSize) {

        Map<String, String> map = new HashMap<>();
        map.put("userDto",userDto.toString());
        map.put("id", userId);
        map.put("name", userName);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        return map;
    }
  • @RequestHeader

获取请求头的信息

@PostMapping("/requestHeader")
    @ResponseBody
    public String requestBody03(@RequestHeader(name = "Content-Type") String contentType){
        return contentType;
    }
  • @CookieValue

获取Cookie的信息

@GetMapping("/demo3")
public void demo3(@RequestHeader(name = "myHeader") String myHeader,
        @CookieValue(name = "myCookie") String myCookie) {
    System.out.println("myHeader=" + myHeader);
    System.out.println("myCookie=" + myCookie);
}
  • @RequestBody

用于获取请求体数据(body), get没有请求体,故而一般用于post请求

@PostMapping("/test01")
    @ResponseBody
    public UserDto test01(@RequestBody UserDto userDto) {
        return userDto;
    }

    @PostMapping("/test02")
    @ResponseBody
    public String test02(@RequestBody String str) {
        return str;
    }
  • @PathVariable

获取路径参数,像url/{id}/{name}这种形式的参数都可以,get获取post请求均可

@PostMapping("/pathVariable/{id}/{name}")
    @ResponseBody
    public Map<String, String> pathVariable(
            @PathVariable(name = "id") String userId,
            @PathVariable(name = "name") String userName) {

        Map<String, String> map = new HashMap<>();
        map.put("id", userId);
        map.put("name", userName);
        return map;
    }
  • @ExceptionHandler

异常统一处理

/** 控制器类的基类 */
public class BaseController {
    /** @ExceptionHandler用于统一处理方法抛出的异常 */
    @ExceptionHandler({ServiceException.class, FileUploadException.class})
    public JsonResult<Void> handleException(Throwable e) {
        JsonResult<Void> result = new JsonResult<Void>(e);
        if (e instanceof UsernameDuplicateException) {
            result.setState(4000);
        } else if (e instanceof UserNotFoundException) {
            result.setState(4001);
        } else if (e instanceof PasswordNotMatchException) {
            result.setState(4002);
        } else if (e instanceof AddressCountLimitException) {
            result.setState(4003);
        } else if (e instanceof AddressNotFoundException) {
            result.setState(4004);
        } else if (e instanceof AccessDeniedException) {
            result.setState(4005);
        } else if (e instanceof ProductNotFoundException) {
            result.setState(4006);
        } else if (e instanceof CartNotFoundException) {
            result.setState(4007);
        } else if (e instanceof InsertException) {
            result.setState(5000);
        } else if (e instanceof UpdateException) {
            result.setState(5001);
        } else if (e instanceof DeleteException) {
            result.setState(5002);
        } else if (e instanceof FileEmptyException) {
            result.setState(6000);
        } else if (e instanceof FileSizeException) {
            result.setState(6001);
        } else if (e instanceof FileTypeException) {
            result.setState(6002);
        } else if (e instanceof FileStateException) {
            result.setState(6003);
        } else if (e instanceof FileUploadIOException) {
            result.setState(6004);
        }
        return result;
    }
}
© 版权声明
THE END
喜欢就支持一下吧
分享