This is a sample Java / Maven / Spring Boot application that can be used as a starter for creating rest api.
Spring boot inbuilt have Tomcat 8 embedded
- 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:
# Using Vs code right click and run or debug config file is setup already
or
# mvn spring-boot:run
- 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.javatrial.DemoApplication.Application : Started Application in 22.285 seconds (JVM running for 09.032)
| Method | Description |
|---|---|
| get/post/put/delete | The service is just a simple usage of Rest methods. |
More interestingly, you can start calling some of the operational endpoints (see full list below) like / (these are available on port 8080)
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 -jarcommand -
Writing a RESTful service using annotation: supports both XML and JSON request / response; simply use desired
Acceptheader 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
-
All APIs are "self-documented" by Swagger2 using annotations
Here are some endpoints you can call:
http://localhost:8091/
http://localhost:8091/api/post
### To view Swagger 2 API docs
Run the server and browse to localhost:8080/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:
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"