IDEA⼊门SpringBoot看这⼀篇就够了,⼿把⼿创建⼯程、开发接⼝、⾃定义配
置、三层。。。
idea快速⼊门SpringBoot
1.创建⼯程
1.1 Spring Initializr
选择SDK:(8)
选择Default
Next
1.2 Project Metadata
Group:输⼊域名反写(com.XXX)
Artifact:输⼊项⽬名(spring-boot-learn)
Type:选择(Maven)
Language:选择语⾔(Java)
Java Version:选择java版本(8)
Descriptio:描述
Next
1.3 Dependencies
Spring Boot:选择版本(2.2.2)
搜索依赖:输⼊依赖名(web)
选择(Spring Web)
Next
1.4 选择⽂件要放置的位置…点击完成
项⽬结构
⼊⼝:SpringBootLearnApplication
配置⽂件:src/main/java/…/springbootlearn/SpringBootLearnApplication.java
测试⼊⼝:src/test/java/com/baby/springbootlearn/SpringBootLearnApplicationTests.java
1.5 修改l
version改为想要的springboot版本(2.2.1.RELEASE)
1.5.1 右键l⽂件>Maven>Reimport
idea可能存在bug,刷新后2.2.1.RELEASE报红
如果可以成功运⾏,则⽆需理会
解读l
spring-boot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
项⽬名称
<groupId>com.baby</groupId>
<artifactId>spring-boot-learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-learn</name>
<description>Demo project for Spring Boot</description> java版本
<properties>
<java.version>1.8</java.version>
</properties>
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
插件
Maven项⽬对于SpringBoot是需要这个插件的
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>
</plugins>
</build>
运⾏项⽬
2.开发接⼝
在com.XXX.springbootlearn下创建包controller
在controller包下创建ParaController
创建的Controller必须放在…Application.java同⽬录下或同⽬录的⼦包下否则会不到
2.1 编写第⼀个接⼝
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: BaBy
* @Date: 2021/3/7 21:01
*/
@RestController
public class ParaController {
@GetMapping("/firstrequest")
public  String firstRequest(){
return "firstRequest";
}
}
运⾏项⽬
浏览器中输⼊localhost:8080/firstrequest访问
页⾯显⽰:firstRequest
2.2 编写获取参数的接⼝
public class ParaController {
...
@GetMapping("/requestpara")
public  String requestpara(@RequestParam Integer num){
return "para from request: "+num;
}
}
运⾏项⽬
浏览器中输⼊localhost:8080/requestpara访问
页⾯显⽰:Whitelabel Error Page。。。
浏览器中输⼊localhost:8080/requestpara?num=5访问
页⾯显⽰:para from request: 5
2.3 编写获取路径参数的接⼝
public class ParaController {
...
@GetMapping("/pathpara/{num}")
public  String pathpara(@PathVariable Integer num){
return "para from path: "+num;
}
}
运⾏项⽬
浏览器中输⼊localhost:8080/pathpara/5访问
页⾯显⽰:para from path: 5
2.4 统⼀前缀接⼝
@RestController
@RequestMapping("pre")
public class ParaController {
...
@GetMapping("/pathpara/{num}")
public  String pathpara(@PathVariable Integer num){
return "para from path: "+num;
}
}
运⾏项⽬
浏览器中输⼊localhost:8080/pre/pathpara/5访问
页⾯显⽰:para from path: 5
2.5 多url的⽤法
@RestController
@RequestMapping("pre")
public class ParaController {
...
@GetMapping({"/multiurl1","/multiurl2"})
public  String multiurl(@RequestParam Integer num){
return "multiurl: "+num;
}
}
运⾏项⽬
浏览器中输⼊localhost:8080/pre/multiurl1?num=5访问
页⾯显⽰:multiurl: 5
浏览器中输⼊localhost:8080/pre/multiurl2?num=5访问
页⾯显⽰:multiurl: 5
2.6 传参默认值
@RestController
@RequestMapping("pre")
public class ParaController {
...
@GetMapping("/required")
public String required(@RequestParam(required = false, defaultValue = "0") Integer num){        return "para from required: "+num;
}
}
运⾏项⽬
浏览器中输⼊localhost:8080/pre/required访问
页⾯显⽰:para from required: 0
浏览器中输⼊localhost:8080/pre/multiurl2?num=5访问
页⾯显⽰:para from required: 5
springboot结构
3.⾃定义配置
3.1配置⽂件
3.1.1 在controller包下创建PropertiesController
package com.ller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: BaBy
* @Date: 2021/3/7 22:06
*/
@RestController
public class PropertiesController {
Integer key1;
Integer key2;
@GetMapping("/getValue")
public String getValue(){
return "key1="+key1+",key2="+key2;
}
}
3.1.2 在application.properties中
key.key1=3
key.key2=4
运⾏项⽬
3.1.3 修改PropertiesController
@RestController
public class PropertiesController {
@Value("${key.key1}")
Integer key1;
@Value("${key.key2}")
Integer key2;
...
}
按住ctrl点击${key1}会跳转到application配置⽂件,说明绑定成功运⾏项⽬
3.2 配置类
3.2.1 创建config包,在config创建keyConfig配置类

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。