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:



  4.0.0
  com.monringstar
  myProject
  0.0.1-SNAPSHOT

  myProject
  http://maven.apache.org

  
    UTF-8
  

  
  	org.springframework.boot
  	spring-boot-starter-parent
  	1.4.1.RELEASE
  
       
            
            spring-snapshots    
            http://repo.spring.io/snapshot    
            true    
            
            
            spring-milestones    
            http://repo.spring.io/milestone    
            
        
        
            
            spring-snapshots    
            http://repo.spring.io/snapshot    
            
            
            spring-milestones    
            http://repo.spring.io/milestone    
            
    
  
    
    	  
	    org.springframework.boot  
	    spring-boot-starter-test  
	    test  
	  
	  
	    org.springframework.boot  
	    spring-boot-starter-web  
	  
	  
	    org.springframework.hateoas  
	    spring-hateoas  
	  
	  
	    org.springframework.boot  
	    spring-boot-starter-actuator  
	
    

搞定后最好先是右键工程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) 简单的跑通了以后就可以继续去搞更多事情了。

你可能感兴趣的:(JAVA)