转载自https://windcoder.com/springbootchutan-chuangjianxiangmu

前言

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

所用环境

IntelliJ IDEA 2017.1.1

JDK1.8

Gradle4.3

Spring Boot1.5.8.RELEASE

创建Gradle项目

1、new Project

如图在gradle中选择java和web

2、填写基础信息

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

3、选择gradle基础

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

相关名词
  • 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

4、标准目录结构

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

添加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) 的异常,以及异常页面的处理

<一> idea+gradle+springboot创建项目的更多相关文章

  1. 1 springboot创建项目

    文章采用idea工具进行springboot项目创建 1点击 New Project 选择[Spring Initializr] 选择Jdk版本其他默认即可 点击Next 2添加项目信息 文章即使用默 ...

  2. Springboot创建项目(idea版本)

    一:概述 由于springboot项目,不管是java工程还是web工程都可以直接以jar方式运行,所以推荐创建jar工程,这里创建jar工程项目为例. 二:两种方式创建springboot项目 1. ...

  3. springboot创建项目

    Springboot作为轻量级快速开发受到无数java人的青睐,Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过 ...

  4. 二:动手实操SpringBoot-使用Spring Initializr创建项目

    使用 Spring Initializr 初始化 Spring Boot 项目 Spring Initializr 从本质上说就是一个Web应用程序,它能为你构建Spring Boot项目结构. 虽然 ...

  5. [转] 使用Spring Boot和Gradle创建项目

    Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...

  6. springBoot(2)---快速创建项目,初解jackson

    快速创建项目,初解jackson 一.快速创建项目 springboot官网提供了工具类自动创建web应用:网址:http://start.spring.io/ 官网页面 1.快速创建一个 选择web ...

  7. IDEA+Maven+多个Module模块(创建多模块SpringBoot整合项目)

    最近在学习springboot,先从创建项目开始,一般项目都是一个项目下会有多个模块,这里先创建一个最简单的实例,一个项目下有一个springboot模块项目提供web服务,引用另一个java项目(相 ...

  8. AndroidStudio创建项目时一直处于building“project name”gradle project info的解决办法

    AndroidStudio创建项目,最后一步finish后,一直长时间处于building“project name”gradle project info,界面就一直停留在如图所示: 谷歌自家的产品 ...

  9. springboot创建maven多模块项目

    SpringBoot创建maven多模块项目 项目结构 该项目名称为springboot-maven-multi,由springboot-maven-multi.user-dao.user-domai ...

随机推荐

  1. No qualifying bean of type xxx' available 的一种解决方法

    获取bean Class beanClass = Class.forName(event.className); FilterEvent filterEvent = (FilterEvent)Bean ...

  2. Github熟悉一

    Code/代码 Commits/提交 Issues/问题 Packages/包装 Marketplace/市场 Topics/话题 Wikis/维基百科 Users/用户 Pull requests/ ...

  3. vue项目-本机ip地址访问

    修改 在 vue项目文件夹中的 package.json scripts >dev 添加 --host 0.0.0.0 "dev": "webpack-dev-se ...

  4. 021:自定义path(或url)转换器

    1.实现如下需求:用户可以根据articles/list/方式获取文章,其中文章分类是采用如下分类,实例如下: 第一种:获取python分类下的文章:/article/python/ 第二种:获取py ...

  5. ORACLE数据库 memory_target SGA 大小

    修改 memory_target 用oracle用户登录,sqlplus "/as sysdba"SQL> show parameters target;     show ...

  6. centos7中yum安装lamp环境

    一.准备工作 1.1 环境 操作系统:centos7(CentOS-7-x86_64-Minimal-1708) 硬件:(这个根据项目运行和配置建议设置,一般我先配个1核1G) 1.2 关闭selin ...

  7. Jira中的Tempo查看component以及issue的工作量汇总

    在右侧group by的地方,同时选中component和issue

  8. ruby的DIR.pwd

    在ruby 中,以下代码可以获得当前脚本的绝对路径: require 'pathname' puts Pathname.new(__FILE__).realpath 将以上代码保存在test1.rb中 ...

  9. Linux 软件安装到哪里合适,目录详解

    文章来源: https://blog.csdn.net/qq_22771739/article/details/83933473 Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有 ...

  10. day35—JavaScript操作元素(创建、删除)

    转行学开发,代码100天——2018-04-20 JavaScript对DOM元素的创建.删除操作. 1.创建DOM元素 appendChild方法 createElement(ochild); op ...