SpringBoot入门之常用注解

来源:互联网 发布:一键抠图软件电脑 编辑:程序博客网 时间:2024/05/29 14:39

 @Transactional     //事务

@Value("${cupSize}")

 private String cupSize;      // 将配置文件中的内容注入到属性变量中

@Component     //注入配置

@ConfigurationProperties(prefix = "girl")   //获取前缀是girl的配置/*把配置写到一个类中*/

@Controller            //处理http请求

@RestController             Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller

@RequestMapping         //配置url映射 

@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)      //可通过/hello或者/hi访问接口


注:单独使用@Controller 需要在pom.xml中配置模板(使用模板在性能上会造成很大的损耗,不推荐使用)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
并且在resources目录下新建一个template文件 在此文件中新建一个 index.html文件
在接口中则只需返回html文件即可 ,
   @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(){ return "index"; }



传单个参数示例:
方案1:
 @RequestMapping(value = {"/{id}/hello","/hi/{id}"},method = RequestMethod.GET)
    public String say(@PathVariable("id") Integer id){
        return  "--->id:"+id;
    }


访问:http://127.0.0.1:8088/girl/100/hello 或者http://127.0.0.1:8088/girl/hi/100


方案2:
    @RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
   //  @GetMapping(value = "/say")
    public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){
        return  "--->id:"+id;
    }
/*
required 是否必填
defaultValue  默认值
@GetMapping简化@RequestMapping
*/

原创粉丝点击