1. env: java 11 + IntelliJ IDEA 2021.3.2 (Community Edition)
2. file->new project->Maven:
pom.xml 需要导入的包:后面三个是jdk8升级到11后,可能会出错,需要用到的包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies>
3.编写MainApplication.java 作为启动类
@SpringBootApplication //@ComponentScan(basePackages= "com/example/controller") 如果controller和启动类不再一个包下则需要使用组件扫描标签用于寻找,或者WhitePage error public class MainApplication { public static void main(String[] args){ SpringApplication.run(MainApplication.class, args); } }
4.编写controller类:
@RestController public class HelloController { @ResponseBody @RequestMapping("/hello") public String HelloController(){ return "Hello this is hello handler 02"; } }
5.run main(), 测试访问地址:
6. application.properties可配置端口等信息
spring.freemarker.checkTemplateLocation=false server.port=8888
7.打包:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.6.RELEASE</version> </parent>
cmd: jave -jar xxx.jar