Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

spring Boot使咱们更容易去建立基于Spring的独立和产品级的能够”即时运行“的应用和服务。支持约定大于配置,目的是尽量快地构建和运行Spring应用。java

以前咱们建立基于Spring的项目须要考虑添加哪些Spring依赖和第三方的依赖。使用Spring Boot后,咱们能够以最小化的依赖开始spring应用。大多数Spring Boot应用须要不多的配置便可运行,好比咱们能够建立独立独立大Java应用,而后经过java -jar运行启动或者传统的WAR部署。其也提供了命令行工具来直接运行Spring脚本(如groovy脚本)。也就是说Spring Boot让Spring应用从配置到运行变的更加简单,但不对Spring自己提供加强(即额外的功能)。git

目的:

让全部Spring开发变得更快,且让更多的人更快的进行Spring入门体验,提供“starter” POM来简化咱们的Maven配置(也就是说使用Spring Boot只有配合maven/gradle等这种依赖管理工具才能发挥它的能力),不像之前,构建一个springmvc项目须要进行好多配置等github

开箱即用,快速开始需求开发而不被其余方面影响(若是可能会自动配置Spring)web

提供一些非功能性的常见的大型项目类特性(如内嵌服务器、安全、度量、健康检查、外部化配置),如能够直接地内嵌Tomcat/Jetty(不须要单独去部署war包)spring

绝无代码生成,且无需XML配置apache

个人构建环境

JDK 7json

Maven 3安全

Servlet3容器 springboot

建立项目

首先使用Maven建立一个普通Maven应用便可,没必要是web的。服务器

 

添加Spring Boot相关POM配置

在pom.xml中添加以下配置:

<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.pcitc.springboot</groupId>
    <artifactId>Springboot</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Springboot Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>0.5.0.BUILD-SNAPSHOT</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable JAR -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!-- Allow access to Spring milestones and snapshots -->
    <!-- (you don't need this if you are using anything after 0.5.0.RELEASE) -->
    <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>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </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>
</project>

继承spring-boot-starter-parent后咱们能够继承一些默认的依赖,这样就无需添加一堆相应的依赖,把依赖配置最小 化;spring-boot-starter-web提供了对web的支持,spring-boot-maven-plugin提供了直接运行项目的插 件,咱们能够直接mvn spring-boot:run运行。

实体

package com.pcitc.entity;

/**
 * <p>
 * User: xxxx
 * <p>
 * Date: 2015年12月24日
 * <p>
 * Version: 1.0
 */
public class User {
    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        User user = (User) o;

        if (id != null ? !id.equals(user.id) : user.id != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}

控制器

package com.pcitc.controller;

import com.pcitc.entity.User;

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

/**
 * <p>
 * User: xxxx
 * <p>
 * Date: 2015年12月24日
 * <p>
 * Version: 1.0
 */
// @EnableAutoConfiguration
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/{id}")
    public User view(@PathVariable("id") Long id) {
        User user = new User();
        user.setId(id);
        user.setName("zhang");
        return user;
    }

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

}

运行  

第一种方式

经过在UserController中加上@EnableAutoConfiguration开启自动配置,而后经过SpringApplication.run(UserController.class);运行这个控制器;这种方式只运行一个控制器比较方便;

第二种方式

经过@Configuration+@ComponentScan开启注解扫描并自动注册相应的注解Bean

package com.pcitc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 * User: xxxxx
 * <p>
 * Date: 2015年12月24日
 * <p>
 * Version: 1.0
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

到此,一个基本的REST风格的web应用就构建完成了。

地址栏输入http://localhost:8080/user/1便可看到json结果。

若是你们查看其依赖,会发现自动添加了须要相应的依赖(无论你用/不用),可是开发一个应用确实变得很是快速,对于想学习/体验Spring的新手,快速创建项目模型等能够考虑用这种方式。固然若是不想依赖这么多的jar包,能够去掉parent,而后本身添加依赖。

附件:spring-boot-demo

参考

https://github.com/spring-projects/spring-boot

相关文章
相关标签/搜索