springBoot初探-创建项目

springBoot初探-创建项目

前言

一边学习公司用到的技术,一边重构小程序后端,从而更好的理解公司搭建的框架。此处记录一个用idea+gradle+springboot的基础实现。

所用环境

IntelliJ IDEA 2017.1.1

JDK1.8

Gradle4.3

Spring Boot1.5.8.RELEASE

创建Gradle项目

1、new Project

如图在gradle中选择java和web

springBoot初探-创建项目

2、填写基础信息

填写GroupId、ArtifactId、Version三项,和maven中一样。

3、选择gradle基础

此处是如图选择,具体含义见下面名词解释:

springBoot初探-创建项目

[toggle hide=“no” title=“相关名词” color=""]

  • Use auto-import | 是否开启自动导入,若开启修改gradle脚本文件后会自动检测变化并对项目进行刷新。
  • Create directories for empty content roots automatically | 导入或者创建gradle项目时,是否自动创建标准目录结构。
  • Create separate module per source set | 让每个模块单独采用Gradle约定的source set结构去执行构建。
  • Use default gradle wrapper (recommended) | 使用Gradle Wrapper(如果一定要翻译的话,可以称之为Gradle 包装器),这可以使得项目组成员不必预先安装好gradle即可执行gradle脚本,同时也便于统一项目所使用的gradle版本,当然虽说是不必预装其实是它会自己去官网帮你下载一个,然而gradle安装包体积不小同时又有墙的过滤,所以开启此项最好事先备好梯子。
  • Use gradle wrapper task configuration | 自定义Gradle Wrapper配置。
  • Use local gradle distribution | 采用本地安装的Gradle执行脚本
  • Gradle home | 选择你的Gradle安装目录即可,无需选择到bin
  • Gradle JVM | 选择构建Gradle项目使用的JVM,默认是项目采用的JDK
[/toggle]

4、标准目录结构

执行完成后需等待一会gradle的自动创建过程,然后可看到如图所示的目录结构

springBoot初探-创建项目

添加SpringBoot依赖

在build.gradle文件中的dependencies 中添加springboot依赖:
 compile("org.springframework.boot:spring-boot-starter-web:1.5.8.RELEASE")
创建含main函数的AppLicationController类:
package com.windcoder.nightbook.controller;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;

/**

  • Description:
  • User: WindCoder
  • Date: 2017-11-12
  • Time: 22<30> 下午 */ @Controller @EnableAutoConfiguration public class AppLicationController {
@ResponseBody
@RequestMapping("/helloMain")
public String home(String name){
return "Hello "+name+"! This is Spring-boot test One";
}
public static void main(String[] args){
SpringApplication.run(AppLicationController.class,args);
}

}

运行

在AppLicationController类处右键-执行run即可。此时不需要管存不存在web.xml,也不需要创建application.properties文件。

新建自定义Controller

到现在为止依旧是spingBoot官方的实例,但此时若自己建一个自定义的controller类,如HelloController 类:
package com.windcoder.nightbook.controller;

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

/**

  • Description:
  • User: WindCoder
  • Date: 2017-11-12
  • Time: 22<32> 下午 */ @Controller @RequestMapping(“/api”) public class HelloController {
@ResponseBody
@RequestMapping(value ="/hello",method = RequestMethod.GET)
public String home(String name){
return "Hello "+name+"! This is Spring-boot test";
}

}

重启执行发现会报如下错误:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

此时需要略作修改才能让程序找到——将@EnableAutoConfiguration替换为@SpringBootApplication:

package com.windcoder.nightbook.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description:
 * User: WindCoder
 * Date: 2017-11-12
 * Time: 22:30 下午
 */
@Controller
@SpringBootApplication
public class AppLicationController {

```
@ResponseBody
@RequestMapping("/helloMain")
public String home(String name){
    return "Hello "+name+"! This is Spring-boot test One";
}

public static void main(String[] args){
    SpringApplication.run(AppLicationController.class,args);
}
```
}

若是该类中没有访问路径,可以将@Controller注解去掉。

还有一种实现方式是对含main的类增加注解,ComponentScan方式:

@Configuration
@EnableAutoConfiguration
@ComponentScan

此处暂且不再重贴代码。

之后重新运行便可访问到自定义Controller中的路径。

弦外音:

很多Spring Boot开发者经常使用@Configuration,@EnableAutoConfiguration,@ComponentScan注解他们的main类,由于这些注解如此频繁地一块使用,Spring Boot就提供了一个方便的@SpringBootApplication注解作为代替。

@SpringBootApplication注解等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan

@SpringBootApplication注解也提供了用于自定义@EnableAutoConfiguration和@ComponentScan属性的别名(aliases)。

参考资料

Spring Boot 官方

[Gradle中文教程系列]-跟我学Gradle-14.1:IDEA中Gradle插件的使用

使用Intellij Idea+Gradle 搭建Java 本地开发环境

SpringBoot配置非含main类的Controller的注解

spring boot 自定义Controller,不能被扫描

 延伸

Spring boot(9) 的异常,以及异常页面的处理

本文采用 CC BY-NC-SA 4.0 许可,转载请注明出处与作者。

支持一下

如果这篇文章对你有帮助,欢迎打赏或分享。

打赏支持

评论