Category: spring

  • How to protect REST API using Basic Authentication?

    How to protect REST API using Basic Authentication?

    REST APIs are one of the primary means of communication between different apps in modern web applications. Anyone can send a request to a public REST API and get a response. This poses security risk. We need only legitimate users to hit our REST APIs and get the information they need. We can do this…

  • How to do server side validation in Spring Boot for REST API requests?

    Let’s say you are creating a REST API to add a batsman profile to a cricket database. The API takes in batsman details like name , age ,number of matches, batting average, experience and runs . Before adding these details to our database you want to validate these details. Like name should be a valid…

  • How to deal with optional request parameters in Spring Boot?

    Let’s say you are creating a REST API in Spring Boot which accepts request parameters. You use the annotation @RequestParam to retrieve the request values. And if user does not supply the request parameter Spring will throw error. What if the request parameter is just an optional parameter? How to handle this in Spring ?…

  • How to secure a REST service in Spring Boot using Basic Authentication?

    Rest Services can be secured in a variety of ways: Basic Authentication , Bearer Authentication , API Keys , Open Id etc. Basic Authentication is the simplest , it uses a user name and password to secure a REST service. In this post let’s see how to protect a REST service using basic authentication in…

  • How to call a REST service from Spring declaratively using OpenFeign?

    If you are using Spring, you are probably using Rest Template provided by Spring to invoke third party REST services. That involves some code , preparing the REST template , populating the parameters required for one of the methods exposed by the REST template and finally invoking one of its specific methods. What if you…

  • How to consume a REST service which accepts form data in Spring?

    Let’s say you want to consume a REST service from your Spring Boot / Spring MVC application. The developer who created the REST service you wanted to consume, developed it in such a way that it accepts only form data. May be he created it to accept data from HTML forms. And now you want…

  • How to write custom queries in Spring Data at method level?

    Say you want to write a custom JPA query for one of your data requirements. Spring Data provides great abstraction over JPA to perform CRUD operations. You can easily retrieve entity objects from database based on field values using methods like ‘findByName’ , ‘findByNameAndAge’ etc in your interface extending Spring Data repositories. If you need…

  • How to load initial data in Spring Boot with and without defining schema?

    Let’s say that you want to populate your database with initial set of values as soon as your spring boot application starts. You may particularly need it if you are using an in memory database like H2. The process differs a bit when you define the schema yourself or let Spring Data define it. Spring…