Embarking on a coding adventure is always exciting, especially when you decide to step out of your comfort zone. In this guide, we’ll delve into the depths of the Spring Framework and RESTful web services, all while exploring the capabilities of Visual Studio Code (VSCode) as our primary IDE—a choice that might raise some eyebrows among IntelliJ enthusiasts. So, buckle up as we navigate through this comprehensive refresher, designed to reignite your passion for Java development and perhaps introduce you to new tools along the way.
Table of Contents
- Introduction
- Setting Sail with the Spring Framework
- Choosing VSCode Over IntelliJ: A Controversial Yet Enlightening Choice
- Crafting Your Development Environment
- Bringing Your Web Services to Life
- Anchoring on Apache Tomcat
- Voyage to AWS EC2
- Scaling the Heights with AWS Elastic Beanstalk
- Fortifying with Spring Security
- Navigating Email Verification
- Exploring the H2 In-Memory Database
- Testing Waters with REST Assured
- Charting APIs with Swagger
- Conclusion
- Explore the Repository
Introduction
Welcome back to the world of Java Spring! Whether it’s been a while since you last dipped your toes into the Spring waters or you’re charting new territories, this guide is here to make the journey enjoyable and enlightening. We’ll dive into the intricacies of RESTful web services, enhance our application with robust security, and even deploy our creations to the cloud—all while using VSCode as our compass. So, grab your favorite coding beverage, and let’s set sail!
Setting Sail with the Spring Framework
The Java Spring Framework is like a trusty ship that’s both sturdy and versatile. It’s designed to handle the heavy lifting of infrastructure, allowing you to focus on crafting your application with finesse. With Spring, you can build applications that are not only powerful but also maintainable and scalable.
Choosing VSCode Over IntelliJ: A Controversial Yet Enlightening Choice
Why VSCode?
In the Java development community, IntelliJ IDEA is often hailed as the premier IDE due to its rich features, intelligent code completion, and seamless integration with Java frameworks. However, for this project, I decided to purely use Visual Studio Code (VSCode). This choice might be seen as controversial, especially among seasoned Java developers who swear by IntelliJ.
Exploring New Horizons
The decision to use VSCode was driven by a desire to explore its capabilities and understand how it stacks up against traditional Java IDEs. VSCode is known for its lightweight nature and extensibility, and I was curious to see how it would perform in a Java-centric environment.
- Learning Opportunity: Using VSCode allowed me to step out of my comfort zone and learn new ways of configuring and managing a Java project.
- Extensibility: With the right extensions, VSCode can be tailored to fit the needs of a Java developer.
- Cross-Platform Consistency: VSCode provides a consistent experience across different operating systems, which can be beneficial in collaborative environments.
Crafting Your Development Environment
Setting Up VSCode for Java Development
Despite its reputation as a general-purpose editor, VSCode can be transformed into a powerful Java IDE with the help of extensions.
Essential Extensions for a Smooth Voyage:
- Extension Pack for Java: This is a must-have pack that includes essential tools like Language Support for Java™ by Red Hat, Debugger for Java, Maven, and more.
- Spring Boot Extension Pack: Provides support for Spring Boot development.
- Spring Initializr Java Support: Easily bootstrap a new Spring Boot project.
- Spring Boot Dashboard: Manage and monitor your Spring Boot applications.
- Test Runner for Java: Run and debug your tests within VSCode.
- Java Prettier Formatter: Keep your code neat and consistent.
- XML Support: For editing XML files, which are common in Java projects.
Preparing Your Ship (Environment):
- Install Maven (for macOS users):
xcode-select --install
brew install maven
With your environment set, and VSCode configured, you’re ready to embark on your coding journey!
Bringing Your Web Services to Life
Starting the Adventure
- Clone the Repository:
git clone https://github.com/seanmayer/java-restful-refresher.git
- Open the Project in VSCode:
- Navigate to the project directory:
bash cd java-restful-refresher - Open VSCode:
bash code .
- Compile and Build: In the VSCode terminal:
mvn install
- Launch the Application: You can run the application directly from VSCode using the Spring Boot Dashboard or through the terminal:
./mvnw spring-boot:run
Running Without Security Token Service (STS)
- Navigate to Your Project Directory:
cd mobile-app-ws
- Install Dependencies and Build:
./mvnw install
- Run the Application:
./mvnw spring-boot:run
Or use the VSCode Run and Debug feature to start your application.
And just like that, your RESTful web services are up and running, all within the VSCode environment!
Anchoring on Apache Tomcat
Deploying as a Java App with Tomcat
- Build Your Project:
./mvnw install
- Locate the JAR File:
cd target
cp mobile-app-ws-0.0.1-SNAPSHOT.jar ~/Desktop
- Run the JAR:
java -jar ~/Desktop/mobile-app-ws-0.0.1-SNAPSHOT.jar
Deploying as a WAR File on Tomcat
- Update
pom.xml:
<packaging>war</packaging>
- Clean and Build:
./mvnw clean
./mvnw install
- Set Up Tomcat:
- Download and Unzip Tomcat
- Make Scripts Executable:
chmod a+x *.sh - Start Tomcat:
./startup.sh
- Deploy Your WAR File:
- Rename to
mobile-app-ws.war - Use the Tomcat Manager to deploy.
Your application is now accessible through Tomcat. Time to test those endpoints!
Voyage to AWS EC2
Setting Up an EC2 Instance
- Launch an Instance:
- Name: DemoAPIServer
- OS: Amazon Linux (Free Tier)
- Type: t2.micro
- Security Groups: Allow SSH, HTTP, HTTPS, and custom TCP port 8080
- Connect via SSH:
ssh -i yourkey.pem ec2-user@your-ec2-dns
Installing Java and Tomcat on EC2
- Update Packages:
sudo yum update -y
- Install Java:
sudo yum install java-1.8.0 -y
- Install Tomcat:
- Download Tomcat:
sudo wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz - Extract and Set Up:
sudo tar xvf apache-tomcat-9.0.65.tar.gz -C /usr/share sudo ln -s /usr/share/apache-tomcat-9.0.65 /usr/share/tomcat9
- Configure and Start Tomcat:
- Create a Service File
- Enable and Start the Service:
sudo systemctl enable tomcat9 sudo systemctl start tomcat9
Deploying Your Application
- Transfer Your WAR File to EC2
- Deploy via Tomcat Manager
- Access Your Application
- Navigate to
http://your-ec2-dns:8080/mobile-app-ws
Your application is now sailing in the cloud!
Scaling the Heights with AWS Elastic Beanstalk
Why Elastic Beanstalk?
Imagine having a crew that handles all the heavy lifting—provisioning, scaling, and monitoring—while you focus on coding. That’s Elastic Beanstalk for you!
Deploying with Elastic Beanstalk
- Set Up Amazon RDS for MySQL
- Create a Database Instance
- Configure Security Groups
- Configure Your Application
- Update
application.propertieswith your RDS credentials.
- Package Your Application:
./mvnw package
- Create an Application in Elastic Beanstalk
- Platform: Tomcat
- Upload Your WAR File
- Launch and Monitor
- Use the Elastic Beanstalk dashboard to monitor your application’s health.
Your application is now not just in the cloud but also scalable and highly available!
Fortifying with Spring Security
Adding Security to Your Application
- Add Dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- Configure Security: Create a
WebSecurityConfigclass to customize security settings. - Implement Roles and Authorities
- Use annotations like
@PreAuthorizeto secure methods.
Enabling CORS
If your frontend is on a different domain or port, you’ll need to enable Cross-Origin Resource Sharing (CORS).
@CrossOrigin(origins = "http://localhost:3000")
Security is not just a feature; it’s a necessity. With Spring Security, you have a robust shield protecting your application.
Navigating Email Verification
Setting Up Amazon SES
- Verify Your Email Address
- Configure SES and SNS for Notifications
- Create topics for bounces and complaints.
- Subscribe your email to these topics.
- Update Your Application
- Integrate AWS SES SDK.
- Send verification emails upon user registration.
Bringing It All Together
When a user signs up:
- They receive a verification email.
- They click the link, which activates their account.
This process not only enhances security but also improves user trust in your application.
Exploring the H2 In-Memory Database
Sometimes, you need a lightweight and fast database for development and testing. Enter H2!
Setting Up H2
- Add Dependency:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
- Configure Application Properties:
spring.datasource.url=jdbc:h2:mem:testdb
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
- Access the H2 Console: Navigate to
http://localhost:8080/h2-console/and connect using the provided credentials.
The H2 database is perfect for quick tests and ensures that your application runs smoothly without the overhead of a full-fledged database.
Testing Waters with REST Assured
Testing APIs can be tedious, but with REST Assured, it’s smooth sailing.
Setting Up REST Assured
- Add Dependency:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
- Write Test Cases:
@Test
public void whenRequested_thenStatus200() {
given()
.when()
.get("/api/endpoint")
.then()
.statusCode(200);
}
Testing ensures that your application is seaworthy before it sets sail in production waters.
Charting APIs with Swagger
Visualizing and documenting your APIs is crucial, and Swagger makes it a breeze.
Integrating Swagger
- Add Dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
- Access Swagger UI: Navigate to
http://localhost:8080/swagger-ui.htmlto see your APIs in all their glory.
Swagger not only documents your APIs but also allows for interactive testing, making it an invaluable tool in your arsenal.
Conclusion
What a journey it’s been! We’ve traversed the landscapes of the Java Spring Framework, delved into RESTful web services, fortified our application with security, and even scaled the peaks of AWS deployment—all while exploring the capabilities of VSCode. This experience was not just about revisiting old concepts but about challenging ourselves to adapt to new tools and environments.
Using VSCode instead of the more traditional IntelliJ IDEA opened up new perspectives and demonstrated that sometimes, stepping outside of the norm can lead to rewarding discoveries.
Explore the Repository
Dive deeper into the code and explore the full project on GitHub:
Java RESTful Refresher Repository
This repository contains all the code samples, configurations, and resources discussed in this guide. Feel free to clone, fork, or star the repo to keep it handy.
Thank you for joining this adventure. Remember, the world of coding is vast and ever-changing, but with curiosity and enthusiasm, every journey becomes a rewarding experience. Happy coding!
📚 Further Reading & Related Topics
If you’re exploring Rediscovering the Java Spring Framework with VSCode, these related articles will provide deeper insights:
• Setting Up a Multi-Module Spring Boot Java Project in Visual Studio Code – Learn how to effectively manage and configure multi-module Spring Boot applications in Visual Studio Code for a seamless development experience.
• Mastering Dependency Management with Maven – Discover how to manage Spring Boot dependencies efficiently using Maven in your Visual Studio Code setup for smooth project builds and updates.









Leave a comment