1.打开 https://start.spring.io/ 新建一个maven的demo,这里选择的是1.5.18的版本
2.将自动生成的demo导入eclipse,直接选中File里的import,注意导入的要是maven project
3.导入成功后mydemo的结构
4.测试一下我们的springboot项目
①添加web依赖,pom.xml文件如下
<?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>1.5.18.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>mydemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mydemo</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>
<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>
②添加测试类TestMydemo,注意此类或者以后要添加的类都要和MydemoApplication.java在同一包,或者在它的子包,方便扫描
在同一包
在它的子包
package com.example.mydemo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestMydemo { @RequestMapping("/hello") public String hello(){ return "Hello Word!mydemo test"; } }
③运行MydemoApplication.java:Run as -->Java Application
④访问:http://localhost:8080/hello