首页 > 代码库 > Spring Boot 1 创建Demo

Spring Boot 1 创建Demo

Spring Boot的主要优点:

  • 为所有Spring开发者更快的入门
  • 开箱即用,提供各种默认配置来简化项目配置
  • 内嵌式容器简化Web项目
  • 没有冗余代码生成和XML配置的要求

入门操作:

1.打开http://start.spring.io/技术分享

下载项目压缩包并解压

2.菜单中选择File->New->Project from Existing Sources

3.选择maven,一路next即可。

技术分享

pom.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.qhong</groupId>    <artifactId>SpringBootDemo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>SpringBootDemo</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.0.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
View Code

引入Web模块

当前的pom.xml内容如下,仅引入了两个模块:

  • spring-boot-starter:核心模块,包括自动配置支持、日志和YAML
  • spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito
<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>

创建com.qhong.web包,创建HelloController.java

package com.qhong.web;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created by Administrator on 2016/9/21 0021. */@RestControllerpublic class HelloController {    @RequestMapping("/hello")    public String Index(){        return "hello world!";    }}

点击运行,默认8080端口,被占用

修改SpringBoot端口

方法1:

@SpringBootApplicationpublic class SpringBootDemoApplication implements EmbeddedServletContainerCustomizer {    public static void main(String[] args) {        SpringApplication.run(SpringBootDemoApplication.class, args);    }    @Override    public void customize(ConfigurableEmbeddedServletContainer container) {        container.setPort(8071);    }}

修改程序入口

2.直接设置application.properties配置文件

server.port=8051

很明显用第二种比较好

技术分享

@RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。 

 

http://blog.didispace.com/spring-boot-learning-1/

http://blog.csdn.net/isea533/article/details/50281151

Spring Boot 1 创建Demo