Home > Java > JavaBase > body text

Create a SpringBoot project using IDEA

hzc
Release: 2020-06-22 14:06:12
Original
2850 people have browsed it

Create a SpringBoot project using IDEA

Use IDEA to create a SpringBoot project

1. Open IDEA, create a new project, and select Spring Initializr

Create a SpringBoot project using IDEA

2. Enter Artifact

Create a SpringBoot project using IDEA

3. Check Web

Create a SpringBoot project using IDEA

4. Click finish to complete

Create a SpringBoot project using IDEA

5. Enter the project and delete the following content

Create a SpringBoot project using IDEA

pom.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.example</groupId>
	<artifactId>springbootdemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springbootdemo</name>
	<description>Demo project for Spring Boot</description>

	<!--起步依赖-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

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

	<dependencies>
		<!--开发web项目相关依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--springboot单元测试-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<!--maven构建-->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
Copy after login

6. Create a HelloController

package com.example;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "hello,this is a springboot demo";
    }
}
Copy after login

7. The SpringbootdemoApplication automatically generated by the program will have an @SpringBootApplication annotation. This annotation is used to indicate that this class is the entry point of the program

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//入口
@SpringBootApplication
public class SpringbootdemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootdemoApplication.class, args);
	}
}
Copy after login

@SpringBootApplication turns on Spring's component scanning and springboot's automatic configuration function, which is equivalent to combining the following three annotations

(1) @Configuration: Table name This class uses Java-based Configuration, use this class as the configuration class

(2) @ComponentScan: Enable annotation scanning

(3) @EnableAutoConfiguration: Enable the automatic configuration function of springboot

8. Run SpringbootdemoApplication class

Create a SpringBoot project using IDEA

##9. Test:

Enter http://localhost:8080/hello

in the address bar

9. Use the method of starting the jar package to start

(1) First enter the directory where the project is located. If it is a mac system, right-click on the project and select Reveal in Finder. For Windows systems, Right-click on the project and select Show in Explorer to open the directory where the project is located

(2) Open the terminal and enter the directory where the project is located

cd /Users/shanml/IdeaProjects/SpringbootDemo

Enter mvn install to build the project

(3) After the build is successful, there will be an additional jar package in the project target folder

(4) Use java -jar springbootdemo-0.0.1 -SNAPSHOT.jar

Start the jar package


## Recommended tutorial: "

java tutorial

"

The above is the detailed content of Create a SpringBoot project using IDEA. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template