spring boot first

環境: 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/,出現

先寫到這裏.

相關文章
相關標籤/搜索