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/ .
This is my @Bean in Controller class, which I want to change to @GetMapping
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
IssuesList response = restTemplate.getForObject(
"https://.../rest/api/2/search?jql=project="+projectId+" AND status in (done) AND issueType in (Story)&expand=changelog",
IssuesList.class);
List<Issues> issuesData = response.getIssuesList();
log.info(issuesData.toString());
};
}
This is my RestTemplate @Bean
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.basicAuthentication(auth,auth2).build();
}
This is the response I get when I start the program
[{key= 'PE-1322', fields= {storyPoints= '3', issueType= 'Story', created= '2020-11-18T09:16:55.816+0000'}}]
I tried changing @Bean at CommandLineRunner to @GetMapping but when I do that, I only get this response.
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
And at localhost:8080 I get an empty JSON {}.
#EDIT: This is my full controller class:
@RestController
public class Controller {
private String auth = "...";
private String auth2 = "...";
private String projectId = "...";
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.basicAuthentication(auth,auth2).build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
IssuesList response = restTemplate.getForObject(
"https://.../rest/api/2/search?jql=project="+projectId+" AND status in (done) AND issueType in (Story)&expand=changelog",
IssuesList.class);
List<Issues> issuesData = response.getIssuesList();
log.info(issuesData.toString());
};
}
}
This is the edited version with @GetMapping
@RestController
public class Controller {
private String auth = "...";
private String auth2 = "...";
private String projectId = "...";
private static final Logger log = LoggerFactory.getLogger(KpiMetricsApplication.class);
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.basicAuthentication(auth,auth2).build();
}
@GetMapping("/")
public String run(RestTemplate restTemplate) throws Exception {
IssuesList response = restTemplate.getForObject(
"https://.../rest/api/2/search?jql%3Dproject%3D"+projectId+"%20AND%20status%20in%20(done)%20AND%20issueType%20in%20(Story)&expand%3Dchangelog",
IssuesList.class);
List<Issues> issuesData = response.getIssuesList();
return issuesData.toString();
}
}
#Final Edit
Thanks to @sarcode, I did it. Here is my updated class:
I made a Resttemplate config class first:
@Configuration
@Slf4j
public class RestConfig {
private String auth = "...";
private String auth2 = "...";
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.basicAuthentication(auth,auth2).build();
}
@Bean
public CommandLineRunner startup() {
return args -> {
log.info("**************************************");
log.info(" Configuring with RestTemplate");
log.info("**************************************");
};
}
}
And updated my controller class like this, the thing that made it work was the @Autowired annotation.
@RestController
public class Controller {
private static final Logger log = LoggerFactory.getLogger(KpiMetricsApplication.class);
private String projectId = "...";
@Autowired
private RestTemplate rest = new RestTemplate();
@GetMapping("/")
public String run() throws Exception {
IssuesList response = rest.getForObject(
"https://.../rest/api/2/search?jql=project="+projectId+ " AND status in (done) AND issuetype in (Story)&expand=changelog",
IssuesList.class);
List<Issues> issuesData = response.getIssuesList();
log.info(issuesData.toString());
return response.toString();
}
}