环境: jdk1.8, eclipse 4.6.1,maven3.3.9java
step1: 新建一个maven web工程,这里不讲怎么建立,详情见上一篇: maven构建web工程.web
step2:改pom.xml以下spring
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zw.firstmaven</groupId>
<artifactId>spring-boot</artifactId>
<version>1.0-SNAPSHOT</version>apache
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>浏览器
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>app
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
eclipse
step 3:maven
建立一个类文件spring-boot
package com.zw.firstmaven;ui
import java.util.Date;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class Application {
@RequestMapping("/")
String home() {
return "Hello World!";
}
@RequestMapping("/now")
String hehe() {
return "如今时间:" + (new Date()).toLocaleString();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这时遇到了个问题,项目没有个人包com.zw.firstmaven,只生成一个firstmaven的包,当我建立个人包
并把类建在下面时,类从package 开始报错 ,后面的Date是没有定义的(我已ctrl+shift+o)。
若是有类建在maven生成的firstmaven包下不会报错。也就是说firstmaven 有jdk环境,而我新建的没有,
昨天新建maven项目时是有的。
怎么没的不得而知,只能解决一下了:
eclipse 中 project-Java Build Path- Libraries选项卡,点add Library ,加入JRE System Library.错误消失。
step 4:
Run项目
console :
浏览器 访问http://localhost:8080/,出现
先写到这里.