转载自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. 实现memcpy()函数及过程总结

    1.为什么会写memcpy 在之前的应聘笔试上遇到一道笔试题,题目要求实现一个my_memcpy函数.函数原型:void * my_memcpy(void *dst, const void *src, ...

  2. 解决在mysql表中删除自增id数据后,再添加数据时,id不会自增1的问题

    https://blog.csdn.net/shaojunbo24/article/details/50036859 问题:mysql表中删除自增id数据后,再添加数据时,id不会紧接.比如:自增id ...

  3. 配置 Kibana

    Products Cloud Services Customers Learn downloads EN Docs Kibana 用户手册 » 搭建 » 配置 Kibana «  在 Windows ...

  4. 对webpack的初步研究2

    Entry Points 如“ 入门”中所述,有多种方法可以entry在webpack配置中定义属性.我们会告诉你,你的方法可以配置的entry属性,除了解释为什么它可能对你有用 Single Ent ...

  5. VSCode必备插件

    通用组件 1.汉化插件 https://marketplace.visualstudio.com/items?itemName=MS-CEINTL.vscode-language-pack-zh-ha ...

  6. 关于Reporting Services网站

    1.http://www.c-sharpcorner.com/search/sql%20server%20reporting%20services 2.https://msdn.microsoft.c ...

  7. Linux负载均衡软件LVS

    linux下的开源负载均衡软件LVS的安装.配置和使用.LVS是一个中国人创建和开发的开放源码项目,利用LVS可以构建高可用.高可靠的负载均衡集群,因此,利用Linux+LVS不但可以假设高性能的负载 ...

  8. 笨办法学Python(learn python the hard way)--练习程序31-35

    下面是练习31-练习35,基于python3 #ex31.py 1 print("You enter a dark room witn two doors. Do you go throug ...

  9. xxxxxxxxxxxxxxxxxxx

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  10. scrapy中的cookies参数详解

    COOKIES_ENABLED 默认: True 是否启用cookiesmiddleware.如果关闭,cookies将不会发送给web server. COOKIES_DEBUG 默认: False ...