`

(转)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.

分享到:
评论

相关推荐

    创建运行maven spring boot工程(使用IntelliJ IDEA以及无集成开发环境)

    在本文中,我们将深入探讨如何使用IntelliJ IDEA创建并运行一个Maven Spring Boot工程,同时也会提及在没有集成开发环境(IDE)的情况下如何进行操作。Spring Boot是Java开发领域中一个非常流行的框架,它简化了...

    Intellij IDEA 搭建Spring Boot项目教程.docx

    3. **项目创建**:在IntelliJ IDEA中创建Spring Boot项目,首先通过File -&gt; New -&gt; Project -&gt; Spring Initializr开始。在此界面中,可以设置Group和Artifact字段,选择所需的依赖,如Web、Data JPA等,并确定项目的...

    IntelliJ IDEA创建的spring boot项目

    **IntelliJ IDEA与Spring Boot的结合** IntelliJ IDEA是一款强大的Java集成开发环境,特别适合于Spring Boot项目的开发。其内置的Spring Boot支持使得创建、运行和调试Spring Boot应用变得简单高效。以下是在...

    在IntelliJ IDEA中通过Spring Boot集成达梦数据库:从入门到精通

    在IntelliJ IDEA中通过Spring Boot集成达梦数据库是一个现代应用程序开发的重要步骤,尤其是在Java生态中。本教程将深入探讨如何实现这一目标,从基础设置到高级应用,帮助你从入门到精通。达梦数据库是一款由中国...

    史上最简单的 IntelliJ IDEA 教程

    史上最简单的 IntelliJ IDEA 教程

    IntelliJ IDEA不重启tomcat,自动部署 热发布Spring Boot

    标题中的“IntelliJ IDEA不重启tomcat,自动部署 热发布Spring Boot”涉及到的是在开发Spring Boot应用时,如何使用IntelliJ IDEA这款强大的Java集成开发环境(IDE)来实现在不重启Tomcat服务器的情况下,进行项目的...

    最新版intellij IDEA插件intellij-spring-helper

    总之,`intellij-spring-helper`插件是Spring和Spring Boot开发者在IntelliJ IDEA中的得力助手,它的存在使得社区版IDEA也能享受到与旗舰版相似的Spring开发体验。通过使用这个插件,开发者能够更专注于业务逻辑的...

    IntellijIdea开发工具详细使用文档.pdf

    IntelliJ Idea开发工具详细使用文档 IntelliJ IDEA是一款功能强大且灵活的集成开发环境(IDE),由 JetBrains 公司开发。它支持多种编程语言,包括 Java、Groovy、Kotlin、Scala 等。下面是 IntelliJ IDEA 的详细...

    IntelliJ IDEA中创建Spring boot项目,定时任务测试

    【Spring Boot与IntelliJ IDEA集成使用】 Spring Boot是由Pivotal团队提供的全新框架,其设计目标是用来简化新Spring应用的初始搭建以及开发过程。它集成了大量常用的第三方库配置,如MySQL、JPA、Thymeleaf、...

    Intellij IDEA 入门教程.pdf (高清文字版)

    IntelliJ IDEA是一款由JetBrains公司开发的Java集成开发环境(IDE),广泛应用于Java开发。本教程主要介绍IntelliJ IDEA的安装与配置、基本使用方法、常见问题解决技巧、以及与Maven、Git、Tomcat等工具的集成。 ...

    IntelliJ IDEA 中文指南.pdf

    文档还包括了一些特殊知识点,例如在IntelliJ IDEA中使用特殊的代码模板Postfix Completion,以及如何利用Maven构建单模块或多模块的Spring MVC、Spring、Mybatis、Spring Data JPA和Spring Boot项目。这些内容对于...

    IDEA社区版SpringBoot插件Spring Assistant

    总的来说,Spring Assistant插件是IDEA社区版中一个不可多得的Spring Boot开发辅助工具,虽然官方停止了对其的支持,但通过社区的力量,我们依然能在最新的版本中享受到它的便利。善用此插件,将让Spring Boot的开发...

    IntelliJ IDEA使用教程从入门到上瘾(2019图文版)

    IntelliJ IDEA 使用教程从入门到上瘾(2019 图文版) 本资源主要介绍了 IntelliJ IDEA 的使用教程,从基础到高级,涵盖了从设置工作空间到配置 Maven 等多个方面的知识点。 1. IDEA VS Eclipse 核心术语比较 在...

    IntelliJ IDEA 2021Maven+SpringBoot项目基本框架.7z

    本资源是关于使用IntelliJ IDEA 2021创建基于Maven和Spring Boot的基本项目框架的教程。Maven是一个项目管理和综合工具,它管理依赖关系,构建生命周期以及项目信息。而Spring Boot则是一个基于Spring框架的高度简化...

    IntelliJ IDEA 2019汉化文件.rar

    IntelliJ IDEA是一款广受欢迎的Java集成开发环境(IDE),由JetBrains公司开发。它以其高效、智能的代码补全、重构工具以及丰富的插件生态而著名。在2019年,IntelliJ IDEA发布了版本2019.1.3,这个版本在前一版的...

    intellij idea 中文语言包resources_zh_CN_IntelliJIDEA_2019.3_r1.jar

    IntelliJ IDEA是java编程语言开发的集成环境,不过官方是英文版的,这是一个汉化包,下载后放至IntelliJ IDEA安装目录的lib目录下就行,重新启动程序就是中文了。

    IntelliJ IDEA安装详解配置教程

    在创建新项目时,选择相应的项目模板,如Java、Spring Boot等。 4. 设置JDK:在创建新项目时,需要指定JDK版本。如果系统中已安装JDK,IDEA会自动检测;若未安装,需先安装JDK,并在IDEA的“Project Structure”中...

    IntelliJ IDEA使用教程

    IntelliJ IDEA是一款由JetBrains公司开发的集成开发环境(IDE),专为Java语言设计,但也支持其他编程语言如Kotlin、Scala、Groovy等。它被广泛认为是目前最好的Java IDE之一。IntelliJ IDEA通过其智能代码助手、...

    Intellij IDEA教程

    IntelliJ IDEA是一款深受开发者喜爱的Java集成开发环境(IDE),尤其在Java Web开发领域有着广泛的应用。本教程将深入探讨IntelliJ IDEA的各项功能,帮助你从新手快速成长为熟练的使用者。 首先,IntelliJ IDEA的...

    LEARNING SPRING BOOT 3.0 - THIRD EDITION

    读者将学习如何使用Spring Boot的命令行接口(CLI)快速启动项目,以及如何利用IDE如IntelliJ IDEA或Eclipse进行开发。 书中还会深入讨论Spring Boot的核心特性,比如自动配置、健康检查、内嵌的Tomcat或Jetty...

Global site tag (gtag.js) - Google Analytics