SpringBoot注解

1:在yml文件中设置三个属性为name,age,heigth[实际上分别为String,int,double类型]

2:通过@Value注解获取配置属性值,在这里指定数据类型.

[如果配置文件中没有配置属性,而在属性上使用了@Value注解,那么服务是无法启动成功的]

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloSpringBoot {
	
	@Value("${name}")
	private String name;
	
	@Value("${age}")
	private Integer age;
	
	@Value("${height}")
	private Double height;
	
	@RequestMapping(value = "/getUser", method = RequestMethod.GET)
	public String say() {
		return "姓名:"+this.name+";年龄:"+age+";身高:"+height;
	}
}

3:访问:

4:配置文件中属性嵌套

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloSpringBoot {

	@Value("${Content}")
	private String Content;
	
	@RequestMapping(value = "/getUser", method = RequestMethod.GET)
	public String say() {
		return "所有内容:"+Content;
	}
}

5:对类使用注解传值

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="person")
public class Person {
	private String name;
	private Integer age;
	public Person() {
		super();
	}
	public Person(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

配置:

server:
  port: 8080
  servlet:
    context-path: /springboot
person:
  name: 小明
  age: 15
  height: 170.05

控制层:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.entity.Person;

@RestController
public class PersonSpringBoot {
    
    @Autowired
    private Person person;
    
    @RequestMapping(value="/getUserInfo",method=RequestMethod.GET)
    public Integer getUserInfo() {
        return person.getAge();
    }
}

访问:

6:多个配置文件

场景:开发环境和sit测试环境的值不同,就需要配置不同的文件,而不是修改同一个配置文件

在默认的application.yml中引入其他配置,这里引入的是生产环境的配置

访问:

8:访问时传参数

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/springboot")
public class PersonSpringBoot {
    
    //http://127.0.0.1:8088/springboot/getInfo1/55
    @RequestMapping(value="/getInfo1/{id}",method=RequestMethod.GET)
    public String getInfo1(@PathVariable("id") Integer requestId) {
        return "1:请求参数Id的值为:"+requestId;
    }
    
    //http://127.0.0.1:8088/springboot/22/getInfo2
    @GetMapping(value="/{id}/getInfo2")
    public String getInfo2(@PathVariable("id") Integer requestId) {
        return "2:请求参数Id的值为:"+requestId;
    }
    
    //http://127.0.0.1:8088/springboot/22/getInfo3
    @PostMapping(value="/{id}/getInfo3")
    public String getInfo3(@PathVariable("id") Integer requestId) {
        return "3:请求参数Id的值为:"+requestId;
    }
    
    //http://127.0.0.1:8088/springboot/getInfo4?id=44
    @PostMapping(value="/getInfo4")
    public String getInfo4(@RequestParam("id") Integer requestId) {
        return "4:请求参数Id的值为:"+requestId;
    }
}

访问



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值