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

猜你喜欢

转载自jameskaron.iteye.com/blog/2408428