Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

第 5 章 Spring Boot

目錄

5.1. Spring Boot Quick start
5.1.1. pom.xml
5.1.2. Controller
5.2. 運行 Spring boot 項目
5.2.1. Maven run
5.2.2. Linux systemd
5.2.3. 傳統 init.d 腳本
5.3. application.properties
5.3.1. PID FILE
5.3.2. 內嵌 tomcat server
5.3.2.1. server.tomcat.basedir
5.3.2.2. server
5.3.2.3. tomcat
5.3.2.4. logging
5.3.2.5. charset
5.3.2.6. Session 配置
5.3.2.7. cookie
5.3.2.8. error 路徑
5.3.2.9. 壓縮傳輸
5.3.2.10. server.servlet.context-path
5.3.2.11. ssl
5.3.3. JSON
5.3.4. SMTP 相關配置
5.3.5. Redis
5.3.6. MongoDB
5.3.7. MySQL
5.3.8. Oracle
5.3.9. default_schema
5.3.10. velocity
5.3.11.
5.4. SpringApplication
5.4.1. @SpringBootApplication
5.4.2. @EnableAutoConfiguration
5.4.3. @ComponentScan
5.4.4. CharacterEncodingFilter
5.4.5. 隱藏 Banner
5.4.6. CORS 跨域請求
5.4.7. 實體與倉庫掃瞄
5.4.8. 列出 Beans
5.5. 編譯用於Tomcat的 War
5.6. Properties
5.6.1. @EnableConfigurationProperties 引用自定義 *.properties 配置檔案
5.6.2. spring.profiles.active 參數切換配置檔案
5.6.3. SpringApplicationBuilder.properties() 方法添加配置項
5.7. Service
5.7.1. Application
5.7.2. 定義介面
5.7.3. 實現介面
5.7.4. 調用 Service
5.8. ViewResolver
5.8.1. Maven
5.8.2. application.properties
5.8.3. Application
5.8.4. IndexController
5.8.5. src/main/webapp/WEB-INF/jsp/index.jsp
5.8.6. 整合模板引擎
5.9. String boot with RestTemplate
5.9.1. 在控製器中完整實例
5.10. Spring boot with Logging
5.10.1. 打印日誌
5.10.2. 日誌輸出級別
5.10.3. 禁止控制台輸出日誌
5.10.4. 標準輸出
5.10.5. 日誌分割
5.10.6. 日誌寫入 MongoDB
5.11. Spring boot with Session share
5.11.1. Redis
5.11.1.1. Maven
5.11.1.2. application.properties
5.11.1.3. Application
5.11.2. 測試 Session
5.11.3. JDBC
5.12. Connection and Statement Pooling
5.12.1. org.apache.tomcat.jdbc.pool.DataSource
5.12.2. druid
5.12.3. c3p0 - JDBC3 Connection and Statement Pooling
5.12.4. dbcp2
5.12.5. bonecp
5.12.6. dbcp2
5.13. Spring boot with MongoDB
5.13.1. Maven
5.13.2. Application
5.13.3. MongoTemplate
5.13.4. Repository
5.14. Spring boot with MySQL
5.14.1. Maven
5.14.2. Resource
5.14.3. Application
5.14.4. JdbcTemplate
5.14.5. CrudRepository
5.15. Spring boot with Oracle
5.15.1. Maven
5.15.2. application.properties
5.15.3. Application
5.15.4. CrudRepository
5.15.5. JdbcTemplate
5.15.6. Controller
5.16. Spring boot with PostgreSQL
5.16.1. pom.xml
5.16.2. application.properties
5.16.3. Application
5.16.4. CrudRepository
5.16.5. JdbcTemplate
5.16.6. Controller
5.16.7. Test
5.17. Spring boot with Email
5.17.1. Maven
5.17.2. Resource
5.17.3. POJO
5.17.4. RestController
5.17.5. Test
5.18. Spring boot with Velocity template
5.18.1. Maven
5.18.2. Resource
5.18.3. Application
5.18.4. RestController
5.18.5. Test
5.19. Spring boot with Spring security
5.19.1. Maven
5.19.2. Reource
5.19.3. Application
5.19.4. WebSecurityConfigurer
5.19.5. RestController
5.19.6. 測試
5.19.7. Spring + Security + MongoDB
5.19.7.1. Account
5.19.7.2. AccountRepository
5.19.7.3. WebSecurityConfiguration
5.20. Spring boot with Oauth2
5.20.1. Maven
5.20.2. Password tools
5.20.3. Server
5.20.3.1. Maven
5.20.3.2. application.properties
5.20.3.3. EnableAuthorizationServer
5.20.3.4. EnableResourceServer
5.20.3.5. Entity Table
5.20.3.6. UserRepository
5.20.3.7. UserService
5.20.3.8. TestRestController
5.20.3.9. 資料庫初始化
5.20.3.10. Test
5.20.4. Client
5.20.4.1. Maven
5.20.4.2. SpringBootApplication
5.20.4.3. ClientRestController
5.20.4.4. Test
5.21. Spring boot with Scheduling
5.21.1. Application.java
5.21.2. Component
5.21.3. 查看日誌
5.21.4. 計劃任務控制
5.22. Spring boot with Cache
5.22.1. Spring boot with Redis
5.22.1.1. maven
5.22.1.2. application.properties
5.22.1.3. JUnit
5.22.1.4. Controller
5.22.2. @Cacheable
5.22.2.1. maven
5.22.2.2. Controller
5.23. Spring boot with Hessian
5.23.1. Maven
5.23.2. Application
5.23.3. HessianServiceExporter
5.23.4. Service
5.23.5. RestController
5.24. Spring boot with Apache Kafka
5.24.1. 安裝 kafka
5.24.2. maven
5.24.3. Spring boot Application
5.24.4. EnableKafka
5.24.5. KafkaListener
5.24.6. 測試
5.24.7. 完整的發佈訂閲實例
5.25. Spring boot with Git version
5.25.1. CommonRestController 公共控製器
5.25.2. VersionRestController 測試控製器
5.25.3. 創建 .gitattributes 檔案
5.26. Spring boot with HTTPS SSL
5.26.1. 生成自簽名證書
5.26.2. application.properties 配置檔案
5.26.3. 啟動 Spring boot
5.26.4. restTemplate 調用實例
5.27. Spring boot with Elasticsearch 2.x
5.27.1. Maven
5.27.2. Application
5.27.3. application.properties
5.27.4. Domain
5.27.5. ElasticsearchRepository
5.28. Spring boot with Elasticsearch 5.5.x
5.28.1. Maven
5.28.2. Application
5.28.3. application.properties
5.28.4. ElasticsearchConfiguration
5.28.5. RestController
5.29. Spring boot with Apache Hive
5.29.1. Maven
5.29.2. application.properties
5.29.3. Configuration
5.30. Spring boot with Phoenix
5.30.1. Maven
5.30.2. application.properties
5.30.3. Configuration
5.31. Spring boot with Data restful
5.31.1. Maven
5.32. Spring boot with ELK(Elasticsearch + Logstash + Kibana)
5.32.1. TCP 方案
5.32.2. Redis 方案
5.32.3. Kafka 方案
5.32.4. Other
5.33. Spring boot with Docker
5.33.1.
5.33.2.
5.33.3.
5.33.4.
5.34. SpringBootTest
5.34.1.
5.35. FAQ
5.35.1. application.properties 參考
5.35.2. org.hibernate.dialect.Oracle10gDialect does not support identity key generation
5.35.3. No identifier specified for entity
5.35.4. Could not read document: Invalid UTF-8 middle byte 0xd0
5.35.5. java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
5.35.6. Field javaMailSender in cn.netkiller.rest.EmailRestController required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
5.35.7. org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "172.16.0.3", user "test", database "test ", SSL off
5.35.8. Spring boot 怎樣顯示執行的SQL語句

注意以下使用 Spring boot 1.5.2

5.1. Spring Boot Quick start

5.1.1. pom.xml

		
<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>api.netkiller.cn</groupId>
	<artifactId>api.netkiller.cn</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Skyline</name>
	<description>skylinechencf@gmail.com</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source />
					<target />
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>		
		
		

5.1.2. Controller

		
package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

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

測試

curl http://127.0.0.1:8080/