Spring MVC

In this short tutorial, I am going to share with you how to enable CrossOrigin in a RESTful Web Service API build with Spring Boot and Spring MVC. And to do that you need to simply annotate a method in your RestController class with @CrossOrigin annotation like in the example below. Enable CrossOrigin @CrossOrigin(origins = “http://localhost:8080”) @GetMapping(path =…

Read More CrossOrigin in RESTful Web Service

In this tutorial, we will create a Spring Boot application which demonstrates how we can add and use the H2 in-memory database in our application. You will learn how an in-memory database like H2 can be used to develop a Spring boot application without an overhead of doing DB configuration on your machine and without…

Read More Using H2 In-memory Database with Spring Boot

In this Spring MVC tutorial, you will learn how to implement pagination in your RESTful Web Services app built with Spring MVC. The code example below will demonstrate how to implement pagination to implement “Get a list of users” registered with our app feature. I assume you already have your Spring MVC RESTful Web Service…

Read More REST Pagination Tutorial with Spring MVC

In this Spring MVC tutorial, you will learn how to make your RESTful Web Service endpoint respond back with either XML or JSON representation. To make your Web Service endpoint be able to respond with either XML or JSON representation include  MediaType.APPLICATION_JSON_VALUE  and MediaType.APPLICATION_XML_VALUE media types into the RequestMapping annotation of your resource method. For example: @GetMapping(path…

Read More Return XML or JSON in Spring MVC Web Service

In this Spring MVC tutorial, you are going to learn how to use the @PostMapping annotation to make your RESTful Web Service Endpoint be able to handle HTTP Post quest and read its JSON or XML body payload. Sending JSON in HTTP Post Request Let’s say we need to send the following JSON in our…

Read More @PostMapping and @RequestBody Example in Spring MVC

In this Spring MVC tutorial, you will learn how to use the @RequestParam annotation to read the URL Query request parameters in your Spring MVC Web Services application. Let’s assume we have the following URL to your Web Service endpoint. http://localhost:8080/api/users?page=1&limit=50 If we break the above URL down into smaller parts then we will see…

Read More @RequestParam Example in Spring MVC

In this Spring MVC tutorial, you will learn how to use a @PathVariable parameter to read the path parameter value. Let’s say we have the following URL: http://localhost:8080/api/users/5jg8hf4ys9fit If we break the above URL down into smaller parts then we will see that: HTTP – is the protocol being used, localhost  – is the domain name,…

Read More @PathVariable Example in Spring MVC

Almost in every RESTful Web Service application, I have to do the DTO to Entity and then Entity to DTO conversion. DTO stands for Data Transfer Object and is a simple Plain Old Java Object which contains class properties and getters and settings methods for accessing those properties. In this blog post, I am going…

Read More DTO to Entity and Entity to DTO Conversion

With this Spring Boot tutorial, I am going to share with how to handle exceptions in your RESTful Web Service application build with Spring Boot. What if Exception Takes Place The good news is that if Exception takes place and your code does not handle it, Spring Boot will handle the Exception for you and will…

Read More Handle Exceptions in Spring Boot RESTful Service

With this short blog post, I am going to share with you a few ways you can read application properties from application.properties file in Spring Boot. I am going to share 3 ways: Read application.properties using Environment object,  Read a property from application.properties file using @ConfigurationProperties and reading a property using the @Value annotation. To demonstrate how…

Read More Reading application.properties in Spring Boot

It is extremely simple to add logging support to your RESTful Web Service application with Spring Boot. To use Spring Boot logging you do not really need to add any additional dependencies or libraries to your web app project other than the org.springframework.boot example of which is below. But if you are working on a Spring Boot…

Read More Spring Boot Logging with LoggerFactory