A basic Spring Boot RESTful web service (This is for basic concepts. Codebase is not production ready). Please read all the comments properly. May start building a boiler-plate on top of this.
-
Specialization Common exception handler for all controllers
basic.exceptions.CustomResponseExceptionHandler
. -
Request Validations Handled inside beans
basic.models.User
with@Valid
at the controller level. Referbasic.exceptions.CustomResponseExceptionHandler.handleMethodArgumentNotValid
. Dependency requiredspring-boot-starter-validation
. -
HATEOAS Allows you to add reference links with the response without hard-coding URIs. See
basic.resources.UserResourcesImpl.fetchOneUser
response having link tobasic.resources.UserResourcesImpl.fetchUsers
method. URIs are picked from the mappings automatically. Dependency requiredspring-boot-starter-hateoas
. -
Internationalization Refer
basic.resources.InternationalizedResources.internationalized
andbasic.resources.InternationalizedResources.internationalizedContext
and all themessages
property files. Make sure to includeAccept-Language
in request header. -
Content Negotiation While making the call ensure to add
Accept: application/xml
and it will return XML instead of JSON. Dependency requiredcom.fasterxml.jackson.dataformat
. -
Swagger Refer springdoc-openapi and include dependency
org.springdoc
. Swagger UI located at swagger-ui. Swagger docs are located at api-docs. Provides server info, paths, schemas and components. For migration from Springfox Swagger 2 to Springdoc Open API look stackoverflow. -
Actuator Provides production read metrics, heap dumps, jvm, loggers, thread dumps, mapping, etc. Refer properties for wildcard web exposure. actuator-docs. Dependency added
spring-boot-starter-actuator
. actuator-endpoint -
HAL Explorer Bundles features of Swagger UI and visualizes raw JSON such as
/actuator
. Dependency addedspring-data-rest-hal-explorer
. Just go to localhost. -
Static filtering To ignore particular data members use @JsonIgnore or @JsonIgnoreProperties. Refer
basic.models.ConfigsStaticFilter
andbasic.resources.FilteredResources
. -
Dynamic Filtering Filters are applied when the beans are fetched. Refer
basic.models.ConfigsDynamicFilter
andbasic.resources.FilteredResources
. -
Versioning Different companies use different approaches. All are discussed under the
versioning
package. -
Basic Authentication Dependency required
spring-boot-starter-security
. Copy of the password displayed in the terminal when the service starts. Currently, the user and password is listed under property file. Make a request using this password and user asuser
under authorization > basic auth. -
H2 database Enable h2-console under property file and go to h2-console. Refer
data.sql
under resource file. JDBC url should be set tojdbc:h2:mem:testdb
.