SpringBoot之demo

官网地址:https://projects.spring.io/spring-boot/

说明:网上好多资源中springboot都是通过maven工具构建的,当然我也不例外,做第一个小demo前,先给你的工具构建maven的环境。

1.创建maven工程:项目结构如下

    说明:我创建了好多次项目结构总是不完整并且报错,项目-->右键 build path 添加 tomcat 即可解决。

   通过次连接可以解决上述问题:https://blog.csdn.net/shunhua19881987/article/details/79270925

 2.项目结构说明  

     src/main/java 程序开发以及主程序入口
     src/main/resources 配置文件
     src/test/java 测试程序
     pom.xml文件是Maven进行工作的主要配置文件。

 3.搭建项目的骨架 

      

Application.java  代码如下

 1 package com.cn;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class Application {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(Application.class, args);
11     }
12 }

FirstDemoController.java  代码如下

 1 package com.cn.controller;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RestController;
 5 
 6 @RestController
 7 public class FirstDemoController {
 8 
 9     @RequestMapping("/first")
10     public String index() {
11         return "This is my first demo!";
12     }
13 
14 }

在target目录下找到pom.properties文件,添加如下内容

 1 #Generated by Maven Integration for Eclipse
 2 #Wed Mar 14 21:54:44 CST 2018
 3 version=0.0.1-SNAPSHOT
 4 groupId=com.cn
 5 m2e.projectName=SpringBootDemo
 6 m2e.projectLocation=E:\workspace\SpringBootDemo
 7 artifactId=SpringBootDemo
 8 
 9 spring.mvc.view.prefix=/pages
10 spring.mvc.view.suffix=.jsp

pom.xml 文件如下

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>com.cn</groupId>
 5     <artifactId>SpringBootDemo</artifactId>
 6     <packaging>war</packaging>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <name>SpringBootDemo Maven Webapp</name>
 9     <url>http://maven.apache.org</url>
10     <parent>
11         <groupId>org.springframework.boot</groupId>
12         <artifactId>spring-boot-starter-parent</artifactId>
13         <version>1.5.1.RELEASE</version>
14     </parent>
15 
16     <dependencies>
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter-web</artifactId>
20         </dependency>
21         
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-devtools</artifactId>
25             <optional>true</optional>
26         </dependency>
27 
28         <dependency>
29             <groupId>junit</groupId>
30             <artifactId>junit</artifactId>
31             <version>3.8.1</version>
32             <scope>test</scope>
33         </dependency>
34     </dependencies>
35     <build>
36         <finalName>SpringBootDemo</finalName>
37         <plugins>
38             <plugin>
39                 <groupId>org.springframework.boot</groupId>
40                 <artifactId>spring-boot-maven-plugin</artifactId>
41                 <configuration>
42                     <fork>true</fork>
43                 </configuration>
44             </plugin>
45         </plugins>
46     </build>
47 </project>

4.运行项目:来到主函数:Application.java  右键  --->Run as -->java Application

浏览器请求:http://localhost:8080/SpringBootDemo/first

。。。。。。。未完待续。

我们不一样
原文地址:https://www.cnblogs.com/bug-mark/p/8570451.html