springboot(一):helloworld

1、搭建环境:

新建一个maven项目,结构如下:


2、添加依赖:

pom.xml内容如下:

<?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>SpringBootDemo</groupId>
	<artifactId>SpringBootDemo</artifactId>
	<version>1.0-SNAPSHOT</version>
        
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
	</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>

3、启动类:

package com.szl;

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

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

4、controller类:

package com.szl.controller;

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

@RestController
public class UserController {

	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String sayHello(){
		return "hello springBoot!";
	}
}

5、访问:

方式一(最常用):运行启动类Application的main方法

启动完成,用浏览器访问:

http://localhost:8080/hello

浏览器返回:

hello springBoot!

方式二:mvn spring-boot:run

cmd进入到项目目录下,执行mvn spring-boot:run

启动成功,访问http://localhost:8080/hello,返回hello springBoot!

方式三:java -jar

cmd进入项目目录下,执行mvn install,完成后,cd target,可以看到有一个.jar文件:SpringBootDemo-1.0-SNAPSHOT.jar

执行:java -jar SpringBootDemo-1.0-SNAPSHOT.jar

启动完成,用浏览器访问:

http://localhost:8080/hello

浏览器返回:

hello springBoot!


猜你喜欢

转载自blog.csdn.net/ynzz123/article/details/80849419