Springboot入门(二)项目配置
来源:互联网 发布:ubuntu 设置用户目录 编辑:程序博客网 时间:2024/06/08 02:56
pom.xml文件相关信息
<!-- 项目的相关信息--> <groupId>com.example</groupId> <artifactId>Test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Test</name> <description>Demo project for Spring Boot</description> <!--SpringBoot的parent配置 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--Springboot web配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Springboot的单元测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--SpringBoot 构建项目时使用的插件配置 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
TestApplication.java
//要启动springboot项目必须添加该注解@SpringBootApplicationpublic class TestApplication { public static void main(String[] args) { //springboot的启动 SpringApplication.run(TestApplication.class, args); }}
HelloWorld.java
1、在类上面条件声明@RestController
2、增加方法@RequestMapping(“/hello”)
@RestControllerpublic class HelloWorld { @RequestMapping(value="/hello",method=RequestMethod.GET) public String say() { return "Hello Springboot"; }}
application.properties
server.port=8081server.context-path=/Test
server.context-path=/Test为浏览网址添加了一个路径
访问地址:http://127.0.0.1:8081/Test/hello
这里推荐使用application.yml文件,application.yml文件写法上更加简单。
server: port: 8082 context-path: /Test
1、注意port:跟8082之间有空格,context-path也是一样有空格。
2、使用yml文件必须进行配置
<!-- 支持 @ConfigurationProperties 注解 --><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
例如:
@RestControllerpublic class HelloController { @Value("${age}") private Integer age ; @RequestMapping(value="/helloctrl", method=RequestMethod.GET) public String say() { return age+""; }}
application.yml
server: port: 8081age: 18
Demo
HelloController.java
@RestControllerpublic class HelloController { @Value("${age}") private Integer age ; @Value("${name}") private String name; @Value("${content}") private String content; @RequestMapping(value="/helloctrl", method=RequestMethod.GET) public String say() { return content; }}
application.yml
server: port: 8081age: 18name: 小芳content: "name:${name} age:${age} "
简化书写方式:
PersonProperties.java
//添加组件@Component//设置前缀是person@ConfigurationProperties(prefix="person")public class PersonProperties { private String name; private Integer 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; }}
application.yml
server: port: 8081person: age: 19 name: 小芳
调用
@RestControllerpublic class HelloController { @Autowired private PersonProperties mPersonProperties; @RequestMapping(value="/helloctrl", method=RequestMethod.GET) public String say() { return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge(); }}
结果图省略
开发环境与测试环境配置
阅读全文
0 0
- Springboot入门(二)项目配置
- springboot入门(二)--springboot常用注解及配置
- SpringBoot入门(二)
- springboot学习二(springboot相关配置)
- springBoot(二)springboot配置读取、配置原理及其视图
- IDEA进行springmvc项目开发二(springboot入门)
- springboot入门(二)初步集成mybatis
- SpringBoot学习(二)——入门
- SpringBoot的入门搭建(二)
- SpringBoot入门二
- SpringBoot简单入门(二)
- SpringBoot简单入门(二)
- Springboot入门项目
- springboot入门(四)springboot默认日志logback配置
- springboot(二)项目构建
- SpringBoot学习-(二)SpringBoot注解和配置
- SpringBoot 入门与配置
- springboot 学习笔记(二)--- properties 配置
- 设置 jupyter notebook 可远程访问
- jsp中引用的jstl 标签库和fmt标签
- HDU6090---Rikka with Graph(2017多校联赛:思路题)
- Ubuntu 14.10 安装 Android studio
- 两个long类型的数值相除问题
- Springboot入门(二)项目配置
- Wireshark
- 使用AFN ,通过post请求上传图片,带请求头
- navicat 连接sqlserver提示要安装 sql server native client
- 视频(1)--->编解码基础知识
- HDU-1029-Ignatius and the Princess IV
- 《Nginx高性能Web服务器详解》阅读笔记1
- laravel自带SMTP邮件发送配置修改
- PHP Ajax 跨域问题最佳解决方案