尝试将 @GetMapping 用于 Spring Java 中的 API 时返回空 JSON

Empty JSON returned when trying to use @GetMapping to an API in Spring Java

我有一个带有记录器的@Bean,returns 它从 JIRA API 获取的 JSON 数据。我目前正在启动程序时记录我的回复。现在我想开始在我的控制器中使用@GetMapping 并希望在我对 localhost:8080/ 进行 GET 请求时记录信息。

这是我在控制器中的@Bean class,我想将其更改为@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());
        };
    }

这是我的 RestTemplate @Bean

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.basicAuthentication(auth,auth2).build();
    }

这是我启动程序时得到的响应

[{key= 'PE-1322', fields= {storyPoints= '3', issueType= 'Story', created= '2020-11-18T09:16:55.816+0000'}}]

我尝试将 CommandLineRunner 中的@Bean 更改为@GetMapping,但是当我这样做时,我只收到此响应。

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

在 localhost:8080 我得到一个空的 JSON {}。

#编辑: 这是我的完整控制器 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());
        };
    }
}

这是用@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();
    }
}


#最终编辑

感谢@sarcode,我做到了。这是我更新的 class:

我先做了一个Resttemplate配置class:


@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("**************************************");
        };
    }
}

并像这样更新了我的控制器 class,使它起作用的是 @Autowired 注释。


@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();
    }
}

如您所说,您将需要一个带有 @GetMapping 的简单 RestController。

@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();

    }
}

作为旁注,我建议将您的 RestTemplate 配置放在其他地方。

为了记录 HTTP Request/Response,您可以通过附加的 link 重新配置 RestTemplate 吗? Spring RestTemplate - how to enable full debugging/logging of requests/responses?

对你有帮助。