1.前戏准备:java
SpringBoot核心jar包:这里直接从Spring官网下载了1.5.9版本.web
jdk:jdk1.8.0_45.spring
maven项目管理工具:3.5版本.apache
tomcat:8.5版本.浏览器
本地仓库:注意settings.xml里面的设置"<localRepository>E:/SpringBoot/repository</localRepository>"红色部分表明仓库的位置.
tomcat
eclipse:jee-neon3版本.app
2.环境设置eclipse
1)将jdk以及maven的环境变量配置好,配好后在dos窗口进行测试:命令为java -version 和 mvn -vmaven
JAVA_HOME:jdk所在目录spring-boot
path:%JAVA_HOME%\bin
clathpath:%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
MAVEN_HOME:maven所在目录
path:%MAVEN_HOME%\bin
2)eclipse设置
jre替换成1.8版本:Windows-->Preferences-->java-->Installed JREs
maven仓库及路径设置:Windows-->Preferences-->maven-->installations 新建一个maven
:Windows-->Preferences-->maven-->user settings 路径都设为仓库的settings.xml所在路径
server:Windows-->Preferences-->server-->RunTime Environments 添加tomcat8.5
3.建立简单的maven项目
建立一个maven start项目
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.sinosoft</groupId>
<artifactId>HelloSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HelloSpring</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
在src/main/java下新建一个Controller类
package com.sinosoft.HelloSpring;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class HelloSpring {
@RequestMapping("/app")
void index(HttpServletResponse res) throws IOException{
res.getWriter().println("Hello World!");
}
public static void main(String[] args) {
SpringApplication.run(HelloSpring.class, args);
}
}
运行main方法,在浏览器中输入http://localhost:8080/app就能获得 Hello World! 的结果
4.浏览器差别可能致使的问题
最开始用的IE8浏览器,代码及报错以下:
@RestController
@SpringBootApplication
public class HelloSpring {
@RequestMapping("/app")
String index(){
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(HelloSpring.class, args);
}
}
但是我在360浏览器上却能够正常运行并出结果,后来经查询资料,获得这是ie8不支持
因此将红色部分方法替换为
void index(HttpServletResponse res) throws IOException{
res.getWriter().println("Hello World!");
}
即可在ie8上完美运行
这只是个开始...