Skip to content

A Spring Boot Project implementing some of the spring boot and spring cloud examples.

Notifications You must be signed in to change notification settings

vkirodian/spring-boot-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Example

This project uses all possible features of Spring Boot.

Installation

Once you have checked out this repository, go to the base directory and build it using maven

maven clean install

Running the application

Once the project is successfully built, you can run the following command:

java -jar .\target\spring-boot-example-1.jar

The application runs on port 8081. http://localhost:8081/test/

You can also provide dev or prod properties file running the application.

Note: If you want to run prod profile, kindly refer HTTPS Support section first

java -jar .\target\spring-boot-example-1.jar --spring.profiles.active=dev

Logs are generated at logs/springbootapp.log

You would need to set authtoken header = 12345678 before hitting any API, alternatively you can comment the code in AuthInterceptor class.

Spring Boot Features Used

You can check logging using

http://localhost:8081/test/testLogging

GET/POST/PUT/DELETE API's

For more details check following java class

com.demo.springboot.controller.WebServiceController 
GET http://localhost:8081/student/students
GET http://localhost:8081/student/students/byId?id=2
PUT http://localhost:8081/student/students/1
body
{
	"id": "1",
	"name": "Johnny Doey"
}
POST http://localhost:8081/student/students
body
{
	"id": "3",
	"name": "Bobby P"
}
DELETE http://localhost:8081/student/students/3

File Upload/Download

Upload API saves the uploaded file at /images directory

POST http://localhost:8081/file/upload

Download API would provide you with an image file from /images/imagefordownload.jpg

GET http://localhost:8081/file/download

Note: The /images directory should be parallel to the applications jar file

Rest Template Example

For more details refer following java class

com.demo.springboot.controller.WebServiceConsumer

Consumes the above defines student/* API's

GET http://localhost:8081/cons/students
POST http://localhost:8081/cons/students
PUT http://localhost:8081/cons/students
DELETE http://localhost:8081/cons/students

ThymeLeaf Example

For more details refer following java class

com.demo.springboot.controller.ThymeLeafController class

Lists all the students in the system, gives you option to edit/delete any student. You can also add new students.

GET http://localhost:8081/tl/index

Uses AJAX calls to access http://localhost:8081/student/students API.

This uses GET call.

http://localhost:8081/tl/view-students

This uses POST call.

http://localhost:8081/tl/add-students

CORS

If you want to test CORS implementation than run two instances of this application. One without any profile(running on port 8081) Another with DEV profile(running on port 8082)

DEV profile instance should be able to server http://localhost:8081/tl/view-students jus fine. But if you comment in com.demo.springboot.configuration.RestConfig class You should get CORS error in browser console.

@Bean
public WebMvcConfigurer corsConfigurer() {
	return new WebMvcConfigurer() {
		@Override
		public void addCorsMappings(CorsRegistry registry) {
			registry.addMapping("/student/**").allowedOrigins("http://localhost:8082");
		}
	};
}

Internationalization

Refer to InternationalizationConfig, LocalizationInterceptor, InterceptorConfig for required code.

http://localhost:8081/tl/locale

Scheduling

For more details refer following java class

com.demo.springboot.scheduler.Scheduler class

HTTPS Support

By default the prod profile runs on HTTPS(port 443). Before you can run in prod profile, generate self-signed certificate.

keytool -genkey -alias mycert -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650

The generated keystore.p12 file should be stored at /keystore directory parallel to the application jar file.

Eureka Client

This application acts as a client for Eureka server running on http://localhost:8761.

This application registers it self with Eureka server as spring-boot-example.

If you want to see this application get registered as a client, you need to run the Eureka Server as well.

Download from : https://github.com/vkirodian/microservices-eureka-demo

If you with to disable Eureka Client, simply comment @EnableEurekaClient from Main class.

Cloud Configuration Client

This application acts as a client for configuration server running on

http://localhost:8888/spring-boot-example/default

To test this you need to download and run Cloud Configuration Server from https://github.com/vkirodian/microservices-config-server-demo.

The following API fetches the properties stored in GitHub. For more details refer README for microservices-config-server-demo.

http://localhost:8081/test/configServerProps

Actuator Support

Access actuator API at

http://localhost:8081/actuator/

Cloud Administrator Client Support

This application has exposed monitoring and management data via Actuator, these data can be accessed using Admin Client support.

Configurations has been added to look for Administrator Server at port 9090.

Download and run https://github.com/vkirodian/microservices-admin-server-demo.

You should be able to see all the monitoring and management data at

http://localhost:9090/

For more details refer ReadMe file at microservices-admin-server-demo project.

Swagger2 Support

You can access the API documentation from Swagger2 at

http://localhost:8081/swagger-ui.html

Email Support

http://localhost:8081/test/sendemail

Before sending email set the following values in properties file: app.sender.email, app.sender.password, app.receiver.email.

You might also have to temporarily provide access to 'Less secure app' in gmail.

Web Socket Support

http://localhost:8081/tl/websocket

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

NA