title: Empty JSON returned when trying to use @GetMapping to an API in Spring Java
body: <p>I have @Bean with a logger that returns the JSON data it gets from a JIRA API. I'm currently logging my response while starting the program. Now I want to start using @GetMapping in my controller and want to log the info whenever I do a GET request on localhost:8080/ .</p>
  <p>This is my @Bean in Controller class, which I want to change to @GetMapping</p>
  <pre class="lang-java prettyprint-override"><code>  @Bean
      public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
          return args -&gt; {
              IssuesList response = restTemplate.getForObject(
                      &quot;https://.../rest/api/2/search?jql=project=&quot;+projectId+&quot; AND status in (done) AND issueType in (Story)&amp;expand=changelog&quot;,
                      IssuesList.class);
  
  
              List&lt;Issues&gt; issuesData = response.getIssuesList();
  
              log.info(issuesData.toString());
          };
      }
  </code></pre>
  <p>This is my RestTemplate @Bean</p>
  <pre class="lang-java prettyprint-override"><code>    @Bean
      public RestTemplate restTemplate(RestTemplateBuilder builder) {
          return builder.basicAuthentication(auth,auth2).build();
      }
  </code></pre>
  <p>This is the response I get when I start the program</p>
  <pre class="lang-json prettyprint-override"><code>[{key= 'PE-1322', fields= {storyPoints= '3', issueType= 'Story', created= '2020-11-18T09:16:55.816+0000'}}]
  </code></pre>
  <p>I tried changing @Bean at CommandLineRunner to @GetMapping but when I do that, I only get this response.</p>
  <pre><code>2021-01-15 16:08:59.261  INFO 36704 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
  2021-01-15 16:08:59.261  INFO 36704 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
  2021-01-15 16:08:59.261  INFO 36704 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
  </code></pre>
  <p>And at localhost:8080 I get an empty JSON {}.</p>
  <p>#EDIT:
  This is my full controller class:</p>
  <pre class="lang-java prettyprint-override"><code>
  @RestController
  public class Controller {
  
      private String auth = &quot;...&quot;;
      private String auth2 = &quot;...&quot;;
      private String projectId = &quot;...&quot;;
  
  
      @Bean
      public RestTemplate restTemplate(RestTemplateBuilder builder) {
          return builder.basicAuthentication(auth,auth2).build();
      }
  
      @Bean
      public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
          return args -&gt; {
              IssuesList response = restTemplate.getForObject(
                      &quot;https://.../rest/api/2/search?jql=project=&quot;+projectId+&quot; AND status in (done) AND issueType in (Story)&amp;expand=changelog&quot;,
                      IssuesList.class);
  
  
              List&lt;Issues&gt; issuesData = response.getIssuesList();
  
              log.info(issuesData.toString());
          };
      }
  }
  </code></pre>
  <p>This is the edited version with @GetMapping</p>
  <pre class="lang-java prettyprint-override"><code>@RestController
  public class Controller {
  
      private String auth = &quot;...&quot;;
      private String auth2 = &quot;...&quot;;
      private String projectId = &quot;...&quot;;
      private static final Logger log = LoggerFactory.getLogger(KpiMetricsApplication.class);
  
  
      @Bean
      public RestTemplate restTemplate(RestTemplateBuilder builder) {
          return builder.basicAuthentication(auth,auth2).build();
      }
  
      @GetMapping(&quot;/&quot;)
      public String run(RestTemplate restTemplate) throws Exception {
              IssuesList response = restTemplate.getForObject(
                      &quot;https://.../rest/api/2/search?jql%3Dproject%3D&quot;+projectId+&quot;%20AND%20status%20in%20(done)%20AND%20issueType%20in%20(Story)&amp;expand%3Dchangelog&quot;,
                      IssuesList.class);
  
  
              List&lt;Issues&gt; issuesData = response.getIssuesList();
  
              return issuesData.toString();
      }
  }
  
  </code></pre>
  <hr />
  <p><strong>#Final Edit</strong></p>
  <p>Thanks to @sarcode, I did it. Here is my updated class:</p>
  <p>I made a Resttemplate config class first:</p>
  <pre class="lang-java prettyprint-override"><code>
  @Configuration
  @Slf4j
  public class RestConfig {
  
      private String auth = &quot;...&quot;;
      private String auth2 = &quot;...&quot;;
  
      @Bean
      public RestTemplate restTemplate(RestTemplateBuilder builder) {
          return builder.basicAuthentication(auth,auth2).build();
      }
  
      @Bean
      public CommandLineRunner startup() {
          return args -&gt; {
              log.info(&quot;**************************************&quot;);
              log.info(&quot;    Configuring with RestTemplate&quot;);
              log.info(&quot;**************************************&quot;);
          };
      }
  }
  
  </code></pre>
  <p>And updated my controller class like this, the thing that made it work was the @Autowired annotation.</p>
  <pre class="lang-java prettyprint-override"><code>
  @RestController
  public class Controller {
  
      private static final Logger log = LoggerFactory.getLogger(KpiMetricsApplication.class);
  
  
      private String projectId = &quot;...&quot;;
  
      @Autowired
      private RestTemplate rest = new RestTemplate();
  
  
      @GetMapping(&quot;/&quot;)
      public String run() throws Exception {
              IssuesList response = rest.getForObject(
                      &quot;https://.../rest/api/2/search?jql=project=&quot;+projectId+ &quot; AND status in (done) AND issuetype in (Story)&amp;expand=changelog&quot;,
                      IssuesList.class);
  
  
              List&lt;Issues&gt; issuesData = response.getIssuesList();
  
              log.info(issuesData.toString());
              return response.toString();
      }
  }
  </code></pre>