Spring Initializer 快速创建 Spring Boot 项目

本文导读

  • 《Spring Boot 入门》中是使用创建Maven项目,然后导入Spring Boot依赖的方式,虽然相比传统做法已经很快了,但是IDEA等主流Java编辑器都支持快速构建Spring项目
  • 就相当于IDEA可以直接新建Java FX项目一样,效率更高

New Project

  • https://start.spring.io/:这是Spring Boot 官网提供的快速构建项目的地址,因为浏览器正确访问时都会很慢,需要翻墙,所以IDEA很可能会报错“连接超时”
  • 解决办法是将默认路径中的https改成http即可




默认项目结构

  • src/main/java下是源码,主程序 "HelloWorldQuick" 已经生成好了,只需要写我们自己的逻辑业务代码,注意Spring 组件默认必须在主程序同包或者其子包下面
  • src/main/resources 下是应用资源
  1. static:保存所有的静态资源,如 以前的 js、css、images、音视频文件等;
  2. templates:保存所有的模板页面,注意 Spring Boot 默认 jar 包使用嵌入式的 Tomcat,默认不支持JSP页面);但可以使用模板引擎,如 freemarker、thymeleaf;
  3. application.properties:Spring Boot 应用的配置文件,用来修改Spring Boot 的默认设置,如Tomcat的默认端口号等;
  • src/test:Maven的测试目录


默认 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>www.wmx.com</groupId>
    <artifactId>helloWorldQuick</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>helloWorldQuick</name>
    <description>Demo project for Spring Boot</description>

    <!-- 依赖继承:spring-boot-starter-parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.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>
        <!-- Spring Boot web启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot单元测试模块-->
        <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>

功能代码


package com.lct.wmx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2018/7/10 0010.
 *
 * @ResponseBody 写在方法上时,说明对应方法直接返回内容给浏览器;
 * 写在类上时,表示整个类中的方法都直接返回内容给浏览器
 */
@ResponseBody
@Controller
public class HelloWorldController {

    @RequestMapping("hello")
    public String hello() {
        return "Hello World quick !";
    }
}

运行测试



  • 修改Spring Boot 内置的端口号




猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/80990845