Maven构建一个最简单的Spring Boot + Spring MVC项目

使用Maven构建一个最简单的Spring MVC + Spring Boot项目,完全基于java config

一、新建一个maven项目,模板使用quickstart
POM.xml配置:


  4.0.0

  com.luenxin.study
  review
  0.0.1-SNAPSHOT
  jar

  review
  http://maven.apache.org
  
  
    org.springframework.boot  
    spring-boot-starter-parent
  	1.3.0.RELEASE
  

  
    UTF-8
    4.2.3.RELEASE
    1.3.0.RELEASE
    8.0.28
  

  
    
      junit
      junit
      3.8.1
      test
    
    
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      org.springframework.boot
      spring-boot-starter-web
      ${spring.boot.version}
    
  
  
  
    review
    
      
        org.springframework.boot
        spring-boot-maven-plugin
        1.3.0.RELEASE
        
          
            
              repackage
            
          
        
      
    
  


Spring Boot 启动类:

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

@SpringBootApplication
public class App {
    public static void main(String[] args){
        System.out.println("Hello World!");
        SpringApplication.run(App.class, args);
    }
}

Spring MVC配置类:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
public class WebConfig {

}

Controller类,注意这里的注解要写@RestController:

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

@RestController
public class HelloController {
	
	@RequestMapping("/hello")
	public String hello() {
		return "hello world";
	}

}



一个最简单的项目完成了,接着进入到项目目录,输入安装命令:
mvn clean install ,此时会把项目打成jar包,放在target目录下
接着输入启动命令:
java -jar target/review.jar --server.port=9000  我习惯用9000做端口
接着在浏览器输入:http://127.0.0.1:9000/hello
搞定!

你可能感兴趣的:(Maven构建一个最简单的Spring Boot + Spring MVC项目)