0

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();
    }
}
  • You should create a class with@Controller annotation and add @GetMapping function there. Take a look: https://spring.io/guides/gs/rest-service/ – Iman H Jan 15 '21 at 15:15
  • Could you update the question and show us your Controller class ? Make sur you have the right annotations too – Youri Jan 15 '21 at 15:17
  • Yea I did that, all the methods I showed in my post are in a Controller class with @RestController annotation. I just added my full controller class to my question. – Carnage6194 Jan 15 '21 at 15:17
  • So you do not need a CommandLineRunner anymore. Change it to public List getissues() prototype – Iman H Jan 15 '21 at 15:20
  • @Carnage6194 Have you tried: ResponseEntity response = restTemplate.exchange("https://.../rest/api/2/search?jql=project="+projectId+" AND status in (done) AND issueType in (Story)&expand=changelog", HttpMethod.GET, null, new ParameterizedTypeReference() { }); log.info(response.getBody()); – Iman H Jan 15 '21 at 15:54
  • @ImanH when I do that I get this: IssuesList(issuesList=[]) – Carnage6194 Jan 15 '21 at 16:25
  • Add a breakpoint to see if the endpoint is called. Check my answer as well, remove the restTemplate. – sarcode Jan 18 '21 at 08:13

2 Answers2

0

You will need a simple RestController with a @GetMapping as you said.

@RestController
public class SimpleController {

   

    @GetMapping("/")
    public String check() {
        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();
        return issuesData.toString();

    }
}

As a side note, I would recommend placing your RestTemplate Config somewhere else.

sarcode
  • 160
  • 1
  • 9
  • I tried this but the JSON is still empty []. I'm debugging right now and it shows me that List issuesData = response.getIssuesList(); is empty. Why is it suddenly empty when I had no problem with @Bean before? – Carnage6194 Jan 15 '21 at 15:32
  • It is hard to help you out, as your edited code sample in the question is missing the GetMapping annotation and still has the Bean annotation, could you keep it up to date, so we can look into the issues ? @Carnage6194 – Youri Jan 15 '21 at 15:53
  • 1
    You are right, I forgot to add the edited version. I just added it. – Carnage6194 Jan 15 '21 at 16:13
  • Hey thanks, I did it. I made a class only for resttemplate config and made an @Autowired in my controller class and then it worked. I'll update my first post with the working code. – Carnage6194 Jan 18 '21 at 13:35
0

In order to log HTTP Request/Response, Could you re-configure RestTemplate by attached link ? Spring RestTemplate - how to enable full debugging/logging of requests/responses?

It will be helpful for you.

Donggeun
  • 19
  • 2