在springjava中尝试使用@getmapping到api时返回空json

a0zr77ik  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(322)

我有@bean和一个记录器,它返回从jiraapi获得的json数据。我正在记录启动程序时的响应。现在我想开始在我的控制器中使用@getmapping,并希望在每次执行get请求时记录信息localhost:8080/ .
这是我的@bean in controller类,我想把它改成@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());
        };
    }

这是我的restemplate@bean

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

这是我启动程序时得到的React

[{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 i 获取空的json{}。

编辑:这是我的完整控制器类:

@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,我做到了。这是我的最新课程:
我先做了一个restemplate配置类:

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

像这样更新了我的控制器类,使它工作的是@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();
    }
}
wz8daaqr

wz8daaqr1#

您将需要一个简单的restcontroller和@getmapping。

@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配置放在其他地方。

gcxthw6b

gcxthw6b2#

为了记录http请求/响应,您可以通过附加的链接重新配置resttemplate吗?SpringRestTemplate-如何启用请求/响应的完全调试/日志记录?
这会对你有帮助的。

相关问题