(一)Spring Boot项目初建

SpringBoot工程搭建

  1.  在eclipse应用市场中搜索并下载springboot插件

2. 建立springboot工程

3.修改项目名称

4. 一直下一步finish完成

如今咱们的springboot项目就创建完成了。java

项目启动

pom.xml

已经自动导入了springboot的依赖包了web

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>Spring01-2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring01-2</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</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>

Spring012Application.java

package com.example.demo;

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

@SpringBootApplication//标注主程序类,说明这是一个springboot应用
public class Spring012Application {

	public static void main(String[] args) {
		//springboot应用跑起来让主程序类跑起来
		SpringApplication.run(Spring012Application.class, args);
	}

}

首先咱们想让他处理游览器的请求,须要在spring

pom.xml中添加web依赖

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

新建一个HelloController的java类apache

咱们要让他处理一个浏览器的请求,在上面标注@Controller,建立一个浏览器的hello请求经过@RequestMapping("/hello"),咱们要将“hello world!”写出去,返回给浏览器,须要注解@ResponseBody。浏览器

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
	
	@ResponseBody
	@RequestMapping("/hello")
	public String hello() {
		return "hello world!";
	}

}

 

application.properties

项目建立好了,如今项目也能跑起来,可是这样使用的就是默认的8080端口,若是想使用其余端口,好比8081,以下设置。tomcat

这样咱们须要在application.properties添加server端口,能够进行修改。springboot

# tomcat 
server.port=8081
#server.context-path=/springboot
server.tomcat.uri-encoding=UTF-8

这样有的时候若是参数太多代码也会很是多,不简洁,因此咱们能够使用YAML来进行配置properties文件app

首先在pom.xml中添加YAML依赖eclipse

<dependency>
     <groupId>org.yaml</groupId>
     <artifactId>snakeyaml</artifactId>
</dependency>

application.ymlmaven

重命名文件application.properties为application.yml文件,注意“.”前面相同的不须要出现两次及以上,参数名和值中间的冒号后有一个空格。

# tomcat
# server.context-path=/springboot
server: 
  port: 8081
  tomcat:
    uri-encoding: UTF-8

在主程序中右键->Run As ->Spring Boot App,运行成功

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-04-18 15:03:35.751  INFO 15892 --- [           main] com.example.demo.Spring012Application    : Starting Spring012Application on DESKTOP-HB9HLS2 with PID 15892 (F:\spring\springworkspace\springspace1\Spring01-2\target\classes started by hp in F:\spring\springworkspace\springspace1\Spring01-2)
2019-04-18 15:03:35.755  INFO 15892 --- [           main] com.example.demo.Spring012Application    : No active profile set, falling back to default profiles: default
2019-04-18 15:03:36.854  INFO 15892 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-04-18 15:03:36.885  INFO 15892 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-04-18 15:03:36.886  INFO 15892 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-18 15:03:37.001  INFO 15892 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-04-18 15:03:37.001  INFO 15892 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1188 ms
2019-04-18 15:03:37.229  INFO 15892 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-18 15:03:37.467  INFO 15892 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2019-04-18 15:03:37.470  INFO 15892 --- [           main] com.example.demo.Spring012Application    : Started Spring012Application in 2.506 seconds (JVM running for 3.452)

浏览器浏览

http://localhost:8081/hello

controller和启动类不在同一个包

若是想让controller和启动类不在同一个包须要在启动类添加注解,将controller所在的包添加到扫描器中

@ComponentScan(basePackages = {"com.example"})
相关文章
相关标签/搜索