1. 程式人生 > >Eclipse+Maven+Spring-boot快速搭建

Eclipse+Maven+Spring-boot快速搭建

最近也是工作變換的原因,開始上手了java,接觸到了spring這個東西,還有更輕量級的spring-boot,今天我們就來簡易搭建一下spring-boot環境和構建一個簡單響應返回。

開啟eclipse,我們知道eclipse裡面已經集成了maven,那就不必麻煩去下載了,如果你的版本里面沒有,那麼請在help->eclipseMarketPlace裡面找來下載安裝。

接下來,file->new->project->MavenProject->next->next->選擇一個建構(方便一點的話就選個quickStartt吧)->next->設定一個GroupId和ArtifactId,其實也就是一個名字,隨便寫吧。構建完成後,就會有一個Maven工程生成了。

接下來就是直接去到Pom.xml裡面進行設定相應的依賴包,具體的xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.monringstar</groupId>
  <artifactId>myProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>myProject</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.4.1.RELEASE</version>
  </parent>
   <repositories>    
        <repository>    
            <id>spring-snapshots</id>    
            <url>http://repo.spring.io/snapshot</url>    
            <snapshots><enabled>true</enabled></snapshots>    
        </repository>    
        <repository>    
            <id>spring-milestones</id>    
            <url>http://repo.spring.io/milestone</url>    
        </repository>    
    </repositories>    
    <pluginRepositories>    
        <pluginRepository>    
            <id>spring-snapshots</id>    
            <url>http://repo.spring.io/snapshot</url>    
        </pluginRepository>    
        <pluginRepository>    
            <id>spring-milestones</id>    
            <url>http://repo.spring.io/milestone</url>    
        </pluginRepository>    
    </pluginRepositories>
  
    <dependencies>
    	<dependency>  
	    <groupId>org.springframework.boot</groupId>  
	    <artifactId>spring-boot-starter-test</artifactId>  
	    <scope>test</scope>  
	</dependency>  
	<dependency>  
	    <groupId>org.springframework.boot</groupId>  
	    <artifactId>spring-boot-starter-web</artifactId>  
	</dependency>  
	<dependency>  
	    <groupId>org.springframework.hateoas</groupId>  
	    <artifactId>spring-hateoas</artifactId>  
	</dependency>  
	<dependency>  
	    <groupId>org.springframework.boot</groupId>  
	    <artifactId>spring-boot-starter-actuator</artifactId>  
	</dependency>
    </dependencies>
</project>
搞定後最好先是右鍵工程Maven->update一下,看下有沒有什麼錯誤,這個過程中相應的依賴包也會下載到工程裡面。

接下來就可以嘗試增加Entity, Controller, 和相應用來啟動的Application:

package com.monringstar.myEntity;

public class Entity {
	private String id;
	private String name;
	
	public String getId() {
		return id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public void setName(String name) {
		this.name = name;
	}
}
package com.monringstar.myProject;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {


    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }


}package com.monringstar.myProject;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.monringstar.myEntity.Entity;

@RestController
@RequestMapping("/1")
public class myController {
	
	@RequestMapping("/{id}")
	public Entity view(@PathVariable("id") String id) {
		Entity entity = new Entity();
		entity.setId(id);
		entity.setName("工口");
		return entity;
	}
}


package com.monringstar.myProject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

}

至此,只是將內容填充好了,接下來就是build了,右鍵工程,run as->Maven build.. 在對應窗口裡面設定Goals:  clean package spring-boot:run 順便為了方便把下面的Skip Tests 也勾上,apply->run 就可以出現結果了,等執行完,開啟瀏覽器輸入對應你設定的url:比如我這裡就是localhost:8080/1/(id) 簡單的跑通了以後就可以繼續去搞更多事情了。