`

(转)How to build a Spring Boot application using IntelliJ IDEA

阅读更多

转自:https://patrickgrimard.io/2014/08/14/how-to-build-a-spring-boot-application-using-intellij-idea/

 

Welcome back!  This tutorial will be a basic introduction to creating a Spring Boot application using IntelliJ IDEA.  No prior knowledge is expected, the main purpose of this post is to help anyone new to Spring get rolling quickly writing Spring applications with Spring Boot in IntelliJ.  For further reading, be sure to check out Spring’s Guide section for other tutorials on writing Spring Boot applications.

Update 2015-08-26: This tutorial has been updated with the latest version of IntelliJ 15 EAP at the time of writing and describes building an app with IntelliJ’s built in support for Spring Initializr.

Create your new project with IntelliJ

Open up IntelliJ and click on the Create New Project option.

IntelliJ IDEA New Project

Next, select Spring Initializr from the project type in the left panel, select your Project SDK and then click Next.  The Initializr Service URL should already be populated.

Spring Initializr Dialog

Next enter your Maven project properties including name, project type, packaging, Java version, group ID, artifact ID and version for your application.  Complete this step by entering a project description and root package.  Then click Next.

New Project Dialog

Next select your Spring Boot version and any Spring Framework dependency your project will require.  All of Spring’s projects are available from web, security, cloud, database and so on.  Click Next once you’ve selected all your dependencies.

Spring Boot Dependencies

The final step is entering your IntelliJ IDEA project settings.  Click Finish when complete.

IDEA Project Settings

At this point, IntelliJ may prompt you to add your pom.xml as a managed Maven project.  Click Add as Maven Project to dismiss this prompt and have your Maven dependencies automatically synced with your IntelliJ build path.  You’ll see that using Spring Boot, there’s many dependencies you don’t have to add yourself thanks to Spring Boot’s starter POMs.

Exploring the Project

Open up your project’s pom.xml and you’ll see what we just built.

<?xml version="1.0" encoding="UTF-8"?>
<projectxmlns="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>com.patrickgrimard</groupId>
    <artifactId>spring-boot-hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-hello-world</name>
    <description>Sample Spring Boot Project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <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>

The generated application class

The following class was generated for us and that’s all you need to kick start your Spring Boot application.

@SpringBootApplication
public classSpringBootHelloWorldApplication{

    publicstaticvoidmain(String[]args){
        SpringApplication.run(SpringBootHelloWorldApplication.class, args);
    }
}

This class although very simple looking has a lot going on.  First, it’s been annotated with the @SpringBootApplication annotation. This declares the class as a @Configuration class for your application.  You can define additional @Bean definitions in it if you like.  It adds @ComponentScan to locate any other beans you define in your project.  Finally it adds @EnableAutoConfiguration in order to enable automatic configuration of your Spring application context by determining what you have available on your classpath.

Create a REST Controller

The next thing you may want to do at this point is add a controller to your application to handle servlet requests.

@RequestMapping("/api")
@RestController
public classWidgetController{

    @RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
    publicWidgetindex(){
        return new Widget("green", 10, 7);
    }
}

The first annotation on this controller is @RequestMapping("/api") and it tells us that this controller will handle all requests beginning with /api in our application.  The second annotation @RestController tells Spring two things.  First this class is a controller bean that should be managed by Spring and will be picked up by component scanning.  Secondly, all methods inherently use @ResponseBody semantics which means the return value will become the body of our servlet response.

I’ve only defined a single method in this controller responding to the GET HTTP method, and it produces JSON output.  My method merely returns a new instance of the Widget class.  Your controller will return something else that’s related to your business case.

Running your Application

At this point, you can run your application simply by right clicking inside your application class and selecting Run SpringBootHelloWorldApplication.main().  Then browse to http://localhost:8080/api and you’ll see the response from the controller and request mapping we just created.

Wrapping Up!

By now, you should know how to create a new Spring Boot application from scratch using IntelliJ, it’s really quite simple with the introduction of Spring Initializr built into the IDE.  I’ve also provided a very brief introduction to writing a Spring Boot app to get you started.

 

If you’d like to view the source code of this application, it’s available on Github https://github.com/pgrimard/spring-boot-hello-world.

分享到:
评论

相关推荐

    subunit-devel-1.4.0-14.el8.x64-86.rpm.tar.gz

    1、文件说明: Centos8操作系统subunit-devel-1.4.0-14.el8.rpm以及相关依赖,全打包为一个tar.gz压缩包 2、安装指令: #Step1、解压 tar -zxvf subunit-devel-1.4.0-14.el8.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm

    TIA-Portal-V19-HSP.zip

    TIA_Portal_V19_HSP.zip

    自己搭建的无人机跟踪实验,主要讲软件,硬件的需要等等,为初学者提供学习建议及需要学习的内容,讲解使用到的代码等.zip

    自己搭建的无人机跟踪实验,主要讲软件,硬件的需要等等,为初学者提供学习建议及需要学习的内容,讲解使用到的代码等.zip

    stunnel-5.56-5.el8-3.x64-86.rpm.tar.gz

    1、文件说明: Centos8操作系统stunnel-5.56-5.el8_3.rpm以及相关依赖,全打包为一个tar.gz压缩包 2、安装指令: #Step1、解压 tar -zxvf stunnel-5.56-5.el8_3.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm

    西门子PLC与ABB变频器Modbus通讯实战:参数读写、启停控制及触摸屏集成

    内容概要:本文详细介绍了西门子S7-1200 PLC与ABB ACS510变频器通过Modbus协议进行通讯的方法。首先讲解了硬件连接,包括RS485通讯线的正确接法和终端电阻的使用。接着深入探讨了PLC程序的设计,涵盖Modbus主站的初始化、参数读写(如频率设定、启停控制)、以及错误处理方法。同时,提供了触摸屏(WinCC Basic)的操作指导,包括变量关联、按钮绑定和数据显示。最后给出了常见问题的解决方案,确保通讯稳定可靠。 适合人群:从事工业自动化领域的工程师和技术人员,尤其是需要进行PLC与变频器通讯调试的工作人员。 使用场景及目标:适用于需要将西门子PLC与ABB变频器进行Modbus通讯的应用场合,帮助工程师快速掌握通讯配置、参数设置、启停控制及触摸屏集成的具体步骤,提高工作效率并减少调试时间。 其他说明:文中提供了详细的代码示例和注意事项,有助于读者更好地理解和应用相关技术。此外,强调了硬件检查的重要性,避免因接线问题导致的通讯失败。

    Zwift离线版-Windows端教程

    Zwift离线版-Windows端教程

    2023-04-06-项目笔记 - 第四百五十一阶段 - 4.4.2.449全局变量的作用域-449 -2025.03.28

    2023-04-06-项目笔记-第四百五十一阶段-课前小分享_小分享1.坚持提交gitee 小分享2.作业中提交代码 小分享3.写代码注意代码风格 4.3.1变量的使用 4.4变量的作用域与生命周期 4.4.1局部变量的作用域 4.4.2全局变量的作用域 4.4.2.1全局变量的作用域_1 4.4.2.449局变量的作用域_449- 2025-03-28

    十六届蓝桥杯单片机模拟赛资源包.zip

    学习资料:十六届蓝桥杯单片机模拟赛资源包

    机器人控制领域的超轨双光与RIC二光PID程序解析及其应用

    内容概要:本文详细解析了超轨双光PID和RIC二光PID两种开源控制程序的设计思路和实现细节。首先介绍了超轨双光PID程序的核心计算方法,包括PID计算、误差获取以及参数整定等方面的内容。接着探讨了RIC二光PID程序的独特之处,如误差合成、参数自适应和遗忘因子的应用。文中强调了积分项防爆处理、微分项灵敏度提升、传感器布局优化等关键技术点,并提供了调试建议和实践经验。此外,还讨论了增量式PID结构、状态观测器、PWM占空比转换等实用技巧。 适合人群:对机器人控制领域感兴趣的初学者和技术爱好者,尤其是希望深入了解PID控制算法的人群。 使用场景及目标:适用于需要理解和实现PID控制算法的实际工程项目,特别是涉及双光传感器的小车控制系统。目标是帮助读者掌握PID控制的基本原理和高级优化技巧,提高系统的稳定性和响应速度。 其他说明:文中提供的代码片段和调试建议非常实用,建议读者在实践中结合这些内容进行实验和调试,以便更好地理解PID控制的工作机制。

    putty0.80中文设置文件本地目录保存版

    putty0.80CN-X64本地记录

    subunit-1.4.0-14.el8.x64-86.rpm.tar.gz

    1、文件说明: Centos8操作系统subunit-1.4.0-14.el8.rpm以及相关依赖,全打包为一个tar.gz压缩包 2、安装指令: #Step1、解压 tar -zxvf subunit-1.4.0-14.el8.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm

    基于Matlab/Simulink的IEEE RBTS BUS4电力系统仿真模型构建与故障分析

    内容概要:本文详细介绍了如何利用Matlab 2016a的Simulink工具箱搭建IEEE RBTS BUS4标准电力系统仿真模型。首先,文章讲解了系统的基本结构和主要元件的参数设置方法,如主变压器、母线、输电线路等。其次,针对测量模块的布置进行了指导,确保能够精确获取电压和电流数据。再次,探讨了故障注入的方法及其对系统的影响,包括三相短路故障的设置和效果分析。此外,还讨论了分布式电源(如光伏)的接入方式以及其对系统稳定性的影响。最后,提供了批量仿真和数据采集的一些实用技巧。 适合人群:从事电力系统研究和技术开发的专业人士,尤其是有一定Matlab/Simulink使用经验的研究人员。 使用场景及目标:①帮助研究人员快速掌握IEEE RBTS BUS4标准系统的建模方法;②提供详细的故障注入和分布式电源接入案例,便于理解和应用;③通过具体实例展示如何优化系统性能,提高仿真精度。 其他说明:文中不仅包含了具体的参数设定和代码片段,还有许多实践经验分享,有助于读者更好地理解和运用所学知识进行实际项目开发。

    zhengquan看看看咯

    zhengquan看看看咯

    计算机概论教学课件.pdf

    计算机概论教学课件.pdf

    LanQiaoCup-master-蓝桥杯刷题项目

    LanQiaoCup-master-蓝桥杯刷题项目

    用matlab实现的mpc模型预测控制,用matlab的quadprog函数实现了线性mpc函数双积分控制,倒立摆控制,车辆云动学模型控制,车辆动力学模型控制

    matlab

    单片机设计 基于C语言的单片机红外遥控系统设计与实现的详细项目实例(含完整的硬件电路设计,程序设计、GUI设计和代码详解)

    内容概要:本文档详细介绍了一款基于C语言的单片机红外遥控系统的设计与实现。项目旨在通过单片机平台实现对家电设备的高效、稳定、低成本的红外遥控控制。系统设计涵盖了硬件电路设计、软件架构、信号处理、功耗管理、抗干扰设计等方面。文中详细介绍了各个功能模块的具体实现,包括系统初始化、红外信号接收与解码、控制逻辑、红外信号发射等。此外,文档还探讨了系统的可扩展性,提出了多项创新和技术改进的方向,如多设备控制、语音识别、无线网络控制、自学习功能等。 适合人群:具备一定单片机基础知识的研发人员,特别是对嵌入式系统设计、红外通信技术感兴趣的工程师。 使用场景及目标:①学习单片机与红外遥控技术的基础理论和实际应用;②掌握嵌入式系统设计的方法和技巧,特别是在信号处理、功耗优化等方面的实践经验;③为智能家居、家庭娱乐系统等领域的产品开发提供参考。 其他说明:文档不仅提供了详细的硬件电路设计和软件代码实现,还包括了GUI设计的要求和具体实现步骤。此外,文档还强调了系统的可扩展性和未来改进方向,如集成更多传感器、云平台与大数据分析、机器学习等先进技术,以提升系统的智能化水平。

    5G IPRAN基站业务组网及关键技术解析

    内容概要:本文详细介绍了5G IPRAN(IP Radio Access Network)基站业务组网的技术背景、关键技术和具体配置。主要内容涵盖IPRAN的基本概念及其在5G时代的必要性,新型IPRAN设备的功能改进和支持的新技术(如SR、FlexE等),以及具体的组网架构和技术细节,包括但不限于DCN自通、PW+L3VPN组网、FlexE配置、Telemetry技术、Segment Routing、EVPN实现方式、MPLS OAM等。此外,文章还深入探讨了IPRAN基站的流量走向、高可靠性和配置要点,特别是A设备、B设备和ER设备的具体配置步骤。 适合人群:具备一定网络工程基础的专业人士,尤其是从事5G网络建设和维护的技术人员。 使用场景及目标:帮助技术人员理解和实施5G IPRAN基站业务组网,确保网络架构的高效性和稳定性,满足5G网络大带宽、低延迟的要求。 其他说明:本文不仅提供了理论知识,还附带了大量的配置示例,便于读者在实践中应用。

    MATLAB/Simulink中永磁同步电机启动与运行模式切换的方案设计(I/F控制、滑模观测器控制)

    内容概要:本文详细介绍了如何利用MATLAB/Simulink实现永磁同步电机(PMSM)从启动到中高速运行的平滑切换。主要内容分为三个部分:首先是I/F控制用于启动阶段,确保电机平稳启动;其次是滑模观测器(SMO)和磁链观测器的应用,用于中高速运行时的状态估计和控制;最后是模式切换的设计,通过状态机和加权平均方法实现两种控制模式之间的无缝衔接。文中提供了具体的MATLAB代码片段和Simulink模块配置,强调了调试技巧和注意事项,如频率斜坡生成、电流补偿、滤波器应用以及速率限制等。 适合人群:对永磁同步电机控制有一定了解的研究人员和技术人员,特别是那些希望深入理解MATLAB/Simulink在电机控制系统中应用的人群。 使用场景及目标:适用于需要设计高效、稳定的PMSM控制系统的研究项目或工业应用。主要目标是掌握I/F控制、滑模观测器和模式切换的具体实现方法,提高系统的动态响应和平稳性。 其他说明:文章不仅提供理论指导,还分享了许多实用的调试经验和优化技巧,帮助读者更好地理解和解决实际工程中的问题。

Global site tag (gtag.js) - Google Analytics