您的位置:首页 > 编程语言 > Java开发

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

2015-12-24 14:44 686 查看
spring Boot使我们更容易去创建基于Spring的独立和产品级的可以”即时运行“的应用和服务。支持约定大于配置,目的是尽可能快地构建和运行Spring应用。

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

目的:

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

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

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

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

我的构建环境

JDK 7

Maven 3

Servlet3容器

创建项目

首先使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: