idea:spring initializr无web勾选,maven方式搭建springboot项目。jdk7创建springboot项目的版本不兼容问题。

2023-05-03,,

一、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

idea:spring initializr无web勾选,maven方式搭建springboot项目。jdk7创建springboot项目的版本不兼容问题。的相关教程结束。

《idea:spring initializr无web勾选,maven方式搭建springboot项目。jdk7创建springboot项目的版本不兼容问题。.doc》

下载本文的Word格式文档,以方便收藏与打印。