# Spring Boot "Microservice" Example Project
This is a sample Java / Maven / Spring Boot (version 1.5.6) application that can be used as a starter for creating a microservice complete with built-in health check, metrics and much more. I hope it helps you.
## How to Run
This application is packaged as a war which has Tomcat 8 embedded. No Tomcat or JBoss installation is necessary. You run it using the ```java -jar``` command.
* Clone this repository
* Make sure you are using JDK 1.8 and Maven 3.x
* You can build the project and run the tests by running ```mvn clean package```
* Once successfully built, you can run the service by one of these two methods:
```
java -jar -Dspring.profiles.active=test target/spring-boot-rest-example-0.5.0.war
or
mvn spring-boot:run -Drun.arguments="spring.profiles.active=test"
```
* Check the stdout or boot_example.log file to make sure no exceptions are thrown
Once the application runs you should see something like this
```
2017-08-29 17:31:23.091 INFO 19387 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8090 (http)
2017-08-29 17:31:23.097 INFO 19387 --- [ main] com.khoubyari.example.Application : Started Application in 22.285 seconds (JVM running for 23.032)
```
## About the Service
The service is just a simple hotel review REST service. It uses an in-memory database (H2) to store the data. You can also do with a relational database like MySQL or PostgreSQL. If your database connection properties work, you can call some REST endpoints defined in ```com.khoubyari.example.api.rest.hotelController``` on **port 8090**. (see below)
More interestingly, you can start calling some of the operational endpoints (see full list below) like ```/metrics``` and ```/health``` (these are available on **port 8091**)
You can use this sample service to understand the conventions and configurations that allow you to create a DB-backed RESTful service. Once you understand and get comfortable with the sample app you can add your own services following the same patterns as the sample service.
Here is what this little application demonstrates:
* Full integration with the latest **Spring** Framework: inversion of control, dependency injection, etc.
* Packaging as a single war with embedded container (tomcat 8): No need to install a container separately on the host just run using the ``java -jar`` command
* Demonstrates how to set up healthcheck, metrics, info, environment, etc. endpoints automatically on a configured port. Inject your own health / metrics info with a few lines of code.
* Writing a RESTful service using annotation: supports both XML and JSON request / response; simply use desired ``Accept`` header in your request
* Exception mapping from application exceptions to the right HTTP response with exception details in the body
* *Spring Data* Integration with JPA/Hibernate with just a few lines of configuration and familiar annotations.
* Automatic CRUD functionality against the data source using Spring *Repository* pattern
* Demonstrates MockMVC test framework with associated libraries
* All APIs are "self-documented" by Swagger2 using annotations
Here are some endpoints you can call:
### Get information about system health, configurations, etc.
```
http://localhost:8091/env
http://localhost:8091/health
http://localhost:8091/info
http://localhost:8091/metrics
```
### Create a hotel resource
```
POST /example/v1/hotels
Accept: application/json
Content-Type: application/json
{
"name" : "Beds R Us",
"description" : "Very basic, small rooms but clean",
"city" : "Santa Ana",
"rating" : 2
}
RESPONSE: HTTP 201 (Created)
Location header: http://localhost:8090/example/v1/hotels/1
```
### Retrieve a paginated list of hotels
```
http://localhost:8090/example/v1/hotels?page=0&size=10
Response: HTTP 200
Content: paginated list
```
### Update a hotel resource
```
PUT /example/v1/hotels/1
Accept: application/json
Content-Type: application/json
{
"name" : "Beds R Us",
"description" : "Very basic, small rooms but clean",
"city" : "Santa Ana",
"rating" : 3
}
RESPONSE: HTTP 204 (No Content)
```
### To view Swagger 2 API docs
Run the server and browse to localhost:8090/swagger-ui.html
# About Spring Boot
Spring Boot is an "opinionated" application bootstrapping framework that makes it easy to create new RESTful services (among other types of applications). It provides many of the usual Spring facilities that can be configured easily usually without any XML. In addition to easy set up of Spring Controllers, Spring Data, etc. Spring Boot comes with the Actuator module that gives the application the following endpoints helpful in monitoring and operating the service:
**/metrics** Shows “metrics” information for the current application.
**/health** Shows application health information.
**/info** Displays arbitrary application info.
**/configprops** Displays a collated list of all @ConfigurationProperties.
**/mappings** Displays a collated list of all @RequestMapping paths.
**/beans** Displays a complete list of all the Spring Beans in your application.
**/env** Exposes properties from Spring’s ConfigurableEnvironment.
**/trace** Displays trace information (by default the last few HTTP requests).
### To view your H2 in-memory datbase
The 'test' profile runs on H2 in-memory database. To view and query the database you can browse to http://localhost:8090/h2-console. Default username is 'sa' with a blank password. Make sure you disable this in your production profiles. For more, see https://goo.gl/U8m62X
# Running the project with MySQL
This project uses an in-memory database so that you don't have to install a database in order to run it. However, converting it to run with another relational database such as MySQL or PostgreSQL is very easy. Since the project uses Spring Data and the Repository pattern, it's even fairly easy to back the same service with MongoDB.
Here is what you would do to back the services with MySQL, for example:
### In pom.xml add:
```
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
### Append this to the end of application.yml:
```
---
spring:
profiles: mysql
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://<your_mysql_host_or_ip>/bootexample
username: <your_mysql_username>
password: <your_mysql_password>
jpa:
hibernate:
dialect: org.hibernate.dialect.MySQLInnoDBDialect
ddl-auto: update # todo: in non-dev environments, comment this out:
hotel.service:
name: 'test profile:'
```
### Then run is using the 'mysql' profile:
```
java -jar -Dspring.profiles.active=mysql target/spring-boot-rest-example-0.5.0.war
or
mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=mysql"
```
# Attaching to the app remotely from your IDE
Run the service with these command line options:
```
mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
or
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Dspring.profiles.active=test -Ddebug -jar target/spring-boot-rest-example-0.5.0.war
```
and then you can connect to it remotely using your IDE. For example, from IntelliJ You have to add remote debug configuration: Edit configuration -> Remote.
# Star History
[![Star History Chart](https://api.star-history.com/svg?repos=khoubyari/spring-boot-rest-example&type=Date)](https://star-history.com/#khoubyari/spring-boot-rest-example&Date)
# Questions and Comments: khoubyari@gmail.com
没有合适的资源?快使用搜索试试~ 我知道了~
Spring Boot REST 示例项目:含多种技术,如内存数据库、Swagger .zip
共20个文件
java:15个
xml:1个
gitignore:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 152 浏览量
2024-11-14
18:05:08
上传
评论
收藏 18KB ZIP 举报
温馨提示
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
资源推荐
资源详情
资源评论
收起资源包目录
Spring Boot REST 示例项目:含多种技术,如内存数据库、Swagger .zip (20个子文件)
spring-boot-rest-example
pom.xml 4KB
src
test
java
com
khoubyari
example
test
HotelControllerTest.java 7KB
main
resources
application.yml 1KB
java
com
khoubyari
example
Application.java 2KB
api
rest
AbstractRestHandler.java 2KB
docs
SwaggerConfig.java 2KB
HotelController.java 5KB
dao
jpa
HotelRepository.java 533B
service
HotelServiceEvent.java 485B
HotelService.java 2KB
ServiceProperties.java 649B
HotelServiceHealth.java 895B
RestControllerAspect.java 766B
exception
DataFormatException.java 482B
ResourceNotFoundException.java 510B
domain
Hotel.java 2KB
RestErrorInfo.java 430B
LICENSE 1KB
.gitignore 144B
README.md 8KB
共 20 条
- 1
资源评论
pk_xz123456
- 粉丝: 1994
- 资源: 735
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功