(一)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"})
相關文章
相關標籤/搜索