Spring Boot Demo Project
Take a look at a Spring Boot-based REST API leveraging Result objects
This demo project demonstrates how to handle and serialize Result
objects within a Spring Boot application. It provides a working example of a "pet store" web service that exposes a REST API for managing pets.
Generating the Project
The project was generated via Spring Initializr including features: web and cloud-feign.
Adding Serialization Support
Then Jackson datatype module for Result objects was manually added as a dependency to serialize and deserialize Result
objects.
We use a @Bean
to register the datatype module.
API Responses
API responses contain a Result
field, encapsulating the outcome of the requested operation.
Results have different success types, depending on the specific endpoint. Failures will be encapsulated as instances of ApiError
.
Controllers
Controllers return instances of ApiResponse
that will be serialized to JSON by Spring Boot.
Since failures are expressed as ApiError
objects, endpoints invariably return HTTP status 200
.
Running the Application
The application can be built and run with Gradle.
This will start a stand-alone server on port 8080.
Testing the Server
Once started, you can interact with the API.
You should see a JSON response like this:
Using Swagger-UI
You can navigate to http://localhost:8080/ to inspect the API using an interactive UI
The full source code for the example application is available on GitHub.
Last updated