<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ayush Singh</title>
    <description>The latest articles on Forem by Ayush Singh (@ayushsingh1610).</description>
    <link>https://forem.com/ayushsingh1610</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1250197%2Fb4273ba6-3321-4c7b-958c-a3e1a721e1d1.png</url>
      <title>Forem: Ayush Singh</title>
      <link>https://forem.com/ayushsingh1610</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ayushsingh1610"/>
    <language>en</language>
    <item>
      <title>Consuming RESTful API services using Rest Template</title>
      <dc:creator>Ayush Singh</dc:creator>
      <pubDate>Mon, 11 May 2026 11:52:45 +0000</pubDate>
      <link>https://forem.com/ayushsingh1610/consuming-restful-api-services-using-rest-template-dh0</link>
      <guid>https://forem.com/ayushsingh1610/consuming-restful-api-services-using-rest-template-dh0</guid>
      <description>&lt;p&gt;One of most important and basic operations in Java Backend is Consuming Rest api. In this blog I use REST Template to demonstrate the task.&lt;/p&gt;

&lt;h2&gt;
  
  
  GET Method
&lt;/h2&gt;

&lt;p&gt;You have to create 2 projects, from one demo we use other's get endpoint.&lt;/p&gt;

&lt;h3&gt;
  
  
  For 1st Project, which only has the name[Your name].
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;You have to add spring-web dependency for web services.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
&amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
&amp;lt;artifactId&amp;gt;spring-boot-starter-webmvc&amp;lt;/artifactId&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;
java&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a MainController class in the controller package.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;@RestController&lt;/code&gt; annotation to make it RESTful web services and instruct it to return the data instead of view.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a Get endpoint return your name.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; package com.example.demo.controller;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RestController;

 @RestController
 public class MainController {

    @GetMapping("/name")
    public String myName() {
        return "Ayush Singh";
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add a variable in &lt;code&gt;application.properties&lt;/code&gt; to change the server port, &lt;code&gt;server.port = 9090&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  For 2nd Project, do the same for dependencies and controller.
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a Rest Template bean OR initialize it in the MainController class.

&lt;code&gt;
private RestTemplate rest;
public MainController() {
 rest = new RestTemplate();
}
&lt;/code&gt;

java&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;hello()&lt;/code&gt; method for showing the greeting and annotate it with &lt;code&gt;@GetMapping("/hello")&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;application.properties&lt;/code&gt; create a variable to store the url like &lt;code&gt;name.service.url = http://localhost:9090&lt;/code&gt;. This will be the url from where we fetch the name of the user.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add a field in the &lt;em&gt;MainController&lt;/em&gt; name &lt;code&gt;baseUrl&lt;/code&gt; to inject the value of the url of another server.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Value("${name.service.url}")
private String baseUrl;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a full URI by adding base url and resource name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now implement the &lt;code&gt;getForObject()&lt;/code&gt; method to get the data from the another server.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String response = rest.getForObject(url, String.class);
&lt;/code&gt;&lt;/pre&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;url&lt;/code&gt; is the full url.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;String.class&lt;/code&gt; is the response type.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;At last return the response variable add with &lt;code&gt;Hello&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return "Hello " + response;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>api</category>
      <category>backend</category>
      <category>java</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Add Basic Authentication (Console - based)</title>
      <dc:creator>Ayush Singh</dc:creator>
      <pubDate>Mon, 11 May 2026 07:27:28 +0000</pubDate>
      <link>https://forem.com/ayushsingh1610/add-basic-authentication-console-based-40ad</link>
      <guid>https://forem.com/ayushsingh1610/add-basic-authentication-console-based-40ad</guid>
      <description>&lt;p&gt;If you just want to see how the authentication looks like in Spring Boot, this blog is for you. &lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Authentication
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create any controller which you want to secure.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@GetMapping("/")
public String helloWorld() {
return "Hello World";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the &lt;code&gt;spring-boot-starter-security&lt;/code&gt; dependency in pom.xml file. It will automatically add the security on all the endpoints.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
&amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
&amp;lt;artifactId&amp;gt;spring-boot-starter-security&amp;lt;/artifactId&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, start the app and you will see a random generated string on the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to any API client (like Postman) and test the endpoint.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;Basic Auth&lt;/code&gt; security for your endpoint.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy the string and use it as a password (Default username is &lt;code&gt;user&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, test the endpoint, it will give you the desired result.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Thank you!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>springsecurity</category>
      <category>spring</category>
      <category>springboot</category>
      <category>java</category>
    </item>
  </channel>
</rss>
