1.Ecliplse创建Maven工程
New Maven Project - Create Simple Project Group Id: com.yyaat Artifact Id: sample Packaging: jar
2.创建pom.xml
<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>
<groupId>com.yyaat</groupId>
<artifactId>sample-helloworld</artifactId>
<version>1</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- springmvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- tomcat 的支持. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
<!-- 仓库 -->
<repositories>
<repository>
<id>aliyunRepository</id>
<name>myRepository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
项目上右键 maven -update project
创建入口Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.println("ヾ(◍°∇°◍)ノ゙ 项目启动成功 ヾ(◍°∇°◍)ノ゙");
}
}
创建一个HelloController 两个Get请求 一个返回json 一个返回字符串
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public Map<String,Object> hello(){
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put("username", "张三");
resultMap.put("msg", "helloworld");
return resultMap;
}
@GetMapping("/greet")
public String greet(){
return "helloworld";
}
}
一.Eclipse中调试运行方式: 运行Application.java 中的main方法
二.打包成jar或war后运行
java -jar xxx.jar
java -jar xxx.war
1.eclipse中使用 maven打包
run as -> maven build...
Goals : package
2.在项目目录执行 maven命令
mvn package
http://localhost:8080/hello 返回结果:{"username":"张三","msg":"helloworld"} http://localhost:8080/greet 返回结果:helloworld
码云源码地址 https://gitee.com/fedo/spring-boot-study/tree/master/sample-helloword