Spring Boot教程一:工程构建

来源:互联网 发布:无添加面膜知乎 编辑:程序博客网 时间:2024/06/07 02:08

工程搭建

本文工程搭建使用的工具:

JDK1.8
Maven
idea

打开idea,新建一个project,然后右键工程,新建一个module,选择Spring Initializr
这里写图片描述

然后next,填写group、artifact ,然后next,

这里写图片描述

选择web,next完成工程创建。
工程目录如下:

- src    -main        -java            -package                -SpringbootApplication        -resouces            - statics            - templates            - application.yml    -test- pom

pom文件如下:

<?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.springcloud</groupId>    <artifactId>springboot</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>springboot</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.9.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-web</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>

修改程序入口类如下:

/** * @author wang_ */@SpringBootApplicationpublic class SpringbootApplication extends SpringBootServletInitializer {    @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {        return application.sources(SpringbootApplication.class);    }    public static void main(String[] args) {        SpringApplication.run(SpringbootApplication.class, args);    }}

修改配置文件如下:

# Server settings (ServerProperties)#端口server.port=8082server.address=127.0.0.1#server.sessionTimeout=30#访问路径名称server.contextPath=/boot

新建一个test.controller包,新建一个web类:

/** * @Package:com.springboot.test.controller * @ClassName:test * @Description:测试web * @Author Shuyu.Wang * @Date 2017-12-07 10:05 **/@RestControllerpublic class TestController {    @RequestMapping(value = "/hello",method = RequestMethod.GET)    public String hello(){        return  "hello Spring boot";    }}

运行程序,看到控制台:
这里写图片描述
说明程序启动成功了。

然后浏览器访问:http://127.0.0.1:8082/boot/hello
返回:

hello Spring boot
第一个spring boot工程完成

原创粉丝点击