1.使用myeclipse搭建springboot
1) File -> New ->Other
选Spring Web 是为了嵌入一个tomcat,会在pom.xml自动增加:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2) 创建好的目录结构
pom.xml报错了
这是由于maven版本问题导致的,解决方法:
<properties> <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version> </properties>
3) 更新项目,文章最后有换源的教程,下载jar包快
现在项目没有报错了
4)SpringBootDemoApplication.java名太长了,我改短点,改成了Application.java,需要重新Update Project ,这步可以忽略
5)新建HelloController.java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController public class HelloController {
@RequestMapping("/hello") public String hello() {
return "Hello World!"; } }
6) 执行Application.java ,选Java Application 或者Spring Boot App运行都可以
7)浏览器访问 http://localhost:8080/hello
8) maven换源 (可选)
Window -> Pregerences
在这个位置的settings.xml内容换成:(没有这个文件则创建,或者放到其他位置,点Browse选择也可以)
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <mirrors> <mirror> <id>aliyun</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> </settings>