Spring Rest Web Service Client and Server

Hey there, developers! In this enhanced tutorial, we’ll build a Spring RESTful web service using Java 21 and Spring Boot 3, complete with an embedded PostgreSQL database. We’ll then create a client and use JUnit with Jersey Test to thoroughly test our service. Let’s get started!

Prerequisites

Before we begin, ensure you have the following installed:

  • Java Development Kit (JDK): Version 21
  • Maven: Version 3.9+ (or Gradle 8+)
  • Your Favorite IDE: IntelliJ IDEA, Eclipse, VS Code, etc.

Part 1: Building the Spring RESTful Web Service (the Server)

We’ll start by setting up our server application with Spring Boot and an embedded PostgreSQL database.

Step 1: Create a New Spring Boot Project

Use Spring Initializr (https://start.spring.io/) to generate a new Spring Boot project. Include the following dependencies:

  • Spring Web: For building RESTful web services.
  • Spring Data JPA: For database interaction.
  • PostgreSQL Driver: For connecting to PostgreSQL.
  • Embedded PostgreSQL: For our embedded database.

Choose Maven as your build tool and Java 21.

Step 2: Configure pom.xml for Embedded PostgreSQL

Ensure your pom.xml includes the necessary dependencies. Spring Boot 3 generally handles much of this, but double-check the embedded PostgreSQL dependency. Here’s an example:

Step 3: Define the Entity

Let’s create a Greeting entity that will be stored in our database:

Step 4: Create a Repository

Create a repository to interact with the database:

Step 5: Create the REST Controller

Now, let’s create our REST controller:

In this controller:

  • We use @Autowired to inject the GreetingRepository.
  • @GetMapping and @PostMapping handle GET and POST requests.
  • We’re interacting with the database using JPA.

Step 6: Application Configuration (Optional)

For Spring Boot 3, the default settings often suffice. If you need specific database configurations, you can use application.properties or application.yml. For example, to set a datasource name (though the embedded DB doesn’t strictly need this):

Part 2: Testing with JUnit and Jersey Test

Now, let’s write a JUnit test using Jersey Test to verify our REST service.

Step 1: Create a Test Class

Create a new test class, e.g., GreetingControllerTest:

Explanation:

  • @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT): Starts the application on a random port to avoid conflicts during testing.
  • @AutoConfigureMockMvc: We don’t use MockMvc here, but it’s often helpful in Spring Boot tests, and including it doesn’t hurt.
  • @TestPropertySource: Overrides the default database configuration to use an in-memory H2 database for testing. This is generally preferred over an embedded Postgres for unit tests, as H2 is faster and lighter-weight. We’ll keep the embedded Postgres for demonstrating the full setup.
  • @LocalServerPort: Injects the port the application is running on.
  • Client and WebTarget: These are Jersey classes for making HTTP requests.
  • ObjectMapper: Used to convert between Java objects and JSON.
  • The @Test methods send requests to the endpoints and assert the responses. We use post() to create new greetings before retrieving them, ensuring our tests are self-contained and don’t rely on pre-existing data.
  • We’re testing the status codes and the content of the responses.

Step 2: Run the Tests

Run the GreetingControllerTest class as a JUnit test. It will start your Spring Boot application, execute the tests, and then shut down the application. All tests should pass.

Part 3: Running the Application

To run the Spring Boot application:

  1. Build the project: mvn clean install
  2. Run the application: mvn spring-boot:run

The application will start, and you can access the endpoints (e.g., using curl or a browser):

  • curl http://localhost:8080/greetings
  • curl -X POST -H "Content-Type: application/json" -d '{"message":"Hello from curl"}' http://localhost:8080/greetings
  • curl http://localhost:8080/greetings/1 (if you’ve created a greeting with ID 1)

Conclusion

In this tutorial, we’ve built a robust Spring RESTful web service using Java 21 and Spring Boot 3, incorporating an embedded PostgreSQL database for persistence. We’ve also implemented comprehensive testing using JUnit and Jersey Test. This approach ensures that your service is not only functional but also reliable and well-tested. Remember to adapt this code to your specific needs and explore the advanced features of Spring Boot and Jersey.

Leave a Comment