# springboot-base-one **Repository Path**: springbook_foundation/springboot-base-one ## Basic Information - **Project Name**: springboot-base-one - **Description**: springboot 初始搭建项目 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-05-23 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## [springboot官方文档](http://spring.io/projects/spring-boot) ## [本项目地址](https://gitee.com/springbook_foundation/springboot-base-one) ## 快速开始 使用 [Spring Initializr](https://start.spring.io/) 引导您的应用程序。 ### 基础maven包 1. spring-boot-starter-parent springboot 启动父包 2. spring-boot-starter-web 开启web服务 3. spring-boot-starter-test 测试类包 4. spring-boot-maven-plugin plugin插件 能够将Spring Boot应用打包为可执行的jar或war文件 ### springBoot项目 目录结构 ```bash │ pom.xml ## 项目依赖maven库 └─src ├─main │ ├─java │ │ └─com │ │ └─base │ │ │ Application.java ## 启动类,启动类不能直接在java目录下 │ │ │ │ │ ├─config ## 项目配置类目录 │ │ │ WebMvcConfig.java ## springbootmvc配置类 │ │ │ │ │ └─controller ## 控制器目录 │ │ IndexController.java ## 测试接口类 │ │ │ └─resources │ │ application.yml ## 或application.properties 应用程序特有配置信息 │ │ │ └─static │ index.html ## 测试html │ └─test ## 测试类目录 └─java ``` ### pom.xml 配置如下 ```xml 4.0.0 springboot-base springboot-base 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE 1.8 UTF-8 -server -Xms64m -Xmx256m -XX:PermSize=64m -XX:MaxPermSize=128m -Dfile.encoding=UTF-8 -Djava.net.preferIPv4Stack=true aliyun-releases http://maven.aliyun.com/nexus/content/groups/public aliyun-repos http://maven.aliyun.com/nexus/content/groups/public org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin ``` ### application.yml 配置 ```yml spring: application: # 服务名 name: basse server: # 服务端口 port: 8001 ``` ### Application.java 启动类 ``` package com.base; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 开发公司:个人 * 版权:个人 *

* Application * @SpringBootApplication(scanBasePackages = {"com.base"}) scanBasePackages 扫描基础类包 * @author 刘志强 * @created Create Time: 2019/5/23 */ @SpringBootApplication(scanBasePackages = {"com.base"}) public class Application { private final Logger logger = LoggerFactory.getLogger(this.getClass()); public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` ### WebMvcConfig.java webMvc配置类 ``` package com.base.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * 开发公司:个人 * 版权:刘志强 *

* WebMvcConfig * * @author 刘志强 * @created Create Time: 2019/2/16 */ @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { //添加资源处理程序,resources资源目录下的static目录下的资源可以被直接访问 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } } ``` ### IndexController.java 测试Controller ``` package com.base.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * 开发公司:个人 * 版权:个人 *

* IndexController * * @author 刘志强 * @created Create Time: 2019/5/23 */ @Controller public class IndexController { // 重定向至首页 @GetMapping("/") public void root(HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.sendRedirect("/index"); } @GetMapping("/index") @ResponseBody public Map index() { Map map = new HashMap(); map.put("作者","刘志强"); return map; } } ``` ### index.html 文件如下 ``` 标题

这是一个HTML页面
``` ### 启动 Application.java 分别访问 ###### http://localhost:8001 ###### http://localhost:8001/index ###### http://localhost:8001/static/index.html