一、idea 使用spring initializr不選擇web搭建springboot項目
1.file => new => project
2.直接next到finish結束。
3.完成后創建controller用例測試
4.pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cc.ash</groupId> <artifactId>boot-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>boot-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 手動添加web依賴,@restController @requestMapping注解等 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
5.application.properties
#端口號修改。默認8080
server.port=
6.springboot入口啟動類
package cc.ash.bootdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @SpringBootApplication @Controller public class BootDemoApplication { @RequestMapping("/boot") @ResponseBody public String bootTest() { return "hello springboot"; } public static void main(String[] args) { SpringApplication.run(BootDemoApplication.class, args); } }
7.新建包下controller測試類
package cc.ash.bootdemo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping(name="springboot test", value = "/test") public String test() { return "booted"; } }
8.運行結果
新建包下controller測試類:訪問頁面
springboot啟動類中訪問404。改requestMapping、rerun,做了很多操作到懷疑人生。重啟idea,世界終於正常了。
能正常訪問的結果如下
二、maven搭建springboot項目
1.jdk7的情況下
2.maven搭建
創建完成后的項目結構和pom文件。一個字就是干凈
3.手動添加pom依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cc.ash</groupId>
<artifactId>boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- =================jdk1.7與springboot版本不兼容======================= -->
<!--<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.myproject.Application</start-class>
</properties>-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 手動添加web依賴,@restController @requestMapping注解等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
maven導入依賴(未選擇自動導入的情況下)。
4.編寫測試用例代碼
1).測試controller:
TestController
package cc.ash.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping(name="maven手動創建springboot",value={"/manual", "mvnManual"}) @ResponseBody public String test() { return "maven manual"; } }
2.啟動入口:
DemoApplication
package cc.ash; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
3.完成后代碼結構
5.訪問測試
6.jdk1.7錯誤留存
不兼容問題
https://blog.csdn.net/wj197927/article/details/79557017
https://www.jianshu.com/p/95d8a0cf0244
idea日志記錄(非控制台日志,控制台日志如上)
7.springboot項目結構導致controller訪問404的問題
自建的所有需要掃描注解的類,必須與啟動類的包平級或者在其之下。如啟動類在包【aa.bb】,其他需要掃描的類必須在【aa.bb】中或【aa.bb】之下的包里。
參考鏈接:
詳細說明的:https://www.cnblogs.com/remember-me/p/10091126.html
直接上解決方法的:https://blog.csdn.net/dong__CSDN/article/details/85342180