java 中getmapping,在Java spring尝试使用@getmapping到API时返回空JSON[通俗易懂]

2022-07-28 19:07:23 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

我有一个带有记录器的@bean,该记录器返回它从JIRA API获得的JSON数据。我当前正在记录启动程序时的响应。现在我想开始在我的控制器中使用@getmapping,并想在localhost:8080/上执行GET请求时记录信息。

这是Controller类中的@bean,我想将其更改为@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 issuesData = response.getIssuesList();

log.info(issuesData.toString());

};

}

null@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}。

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

}

@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 issuesData = response.getIssuesList();

log.info(issuesData.toString());

};

}

}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/128761.html原文链接:https://javaforall.cn

0 人点赞