【问题标题】:Unable to Get Data from Github Api in Grails App无法从 Grails 应用程序中的 Github Api 获取数据
【发布时间】:2021-09-09 16:45:12
【问题描述】:

我正在尝试在 Grails 控制器中向 Github Api 发出 http 请求。 我昨天刚开始学习 Grails,但我被卡住了。我在互联网上搜索了几个小时,但似乎互联网上关于 Grails 的讨论很少。 我只是想调用 Github Api 并获取用户数据。我熟悉 Api Endpoint,我已经将它与其他框架一起使用过。但我无法在 Grails 中弄清楚这个(也许)小问题。 谁能帮助我如何在 Grails 控制器中进行 API 调用?

在此先感谢并为一个幼稚的问题道歉。

【问题讨论】:

  • 有很多方法可以从 Grails 应用程序调用 API。如果您有任何特定问题并且可以清楚地表达出来,那么可能会提供具体的答案。一些相关信息可在guides.grails.org/grails-micronaut-http/guide/index.html 获得。
  • “但我无法在 Grails 中解决这个(也许)小问题。” - 你能描述一下你尝试了什么以及出了什么问题吗?

标签: github grails grails-plugin


【解决方案1】:

无法从 Grails 应用程序中的 Github Api 获取数据

github.com/jeffbrown/tauseefahmedgithubapi查看项目。

src/main/groovy/tauseefahmedgithubapi/GitHubClient.groovy

package tauseefahmedgithubapi

import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Header
import io.micronaut.http.client.annotation.Client

import static io.micronaut.http.HttpHeaders.USER_AGENT

@Client('https://api.github.com/')
interface GitHubClient {

    @Get('/orgs/{org}/repos')
    @Header(name = USER_AGENT, value = 'Micronaut Demo Application')
    List<GitHubRepository> listRepositoriesForOrginazation(String org)
}

src/main/groovy/tauseefahmedgithubapi/GitHubRepository.groovy

package tauseefahmedgithubapi

import io.micronaut.core.annotation.Introspected

@Introspected
class GitHubRepository {
    String name
}

grails-app/init/tauseefahmedgithubapi/BootStrap.groovy

package tauseefahmedgithubapi


import org.springframework.beans.factory.annotation.Autowired

class BootStrap {

    @Autowired
    GitHubClient client

    def init = { servletContext ->
        def repos = client.listRepositoriesForOrginazation('micronaut-projects')
        for(def repo : repos) {
            log.info "Repo Name: ${repo.name}"
        }
    }
    def destroy = {
    }
}

在应用程序启动时,您可能会看到如下输出:

2021-06-28 13:03:16.834  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: static-website
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: presentations
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-core
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-profiles
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-guides-old
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-examples
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: static-website-test
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-docs
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-test
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-kotlin
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-spring
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-oauth2
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-liquibase
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-flyway
2021-06-28 13:03:16.836  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-elasticsearch
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-graphql
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-grpc
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-kafka
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-netflix
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-groovy
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-micrometer
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-sql
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-mongodb
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-redis
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-neo4j
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-rabbitmq
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-aws
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-rss
2021-06-28 13:03:16.837  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-gcp
2021-06-28 13:03:16.838  INFO --- [  restartedMain] tauseefahmedgithubapi.BootStrap          : Repo Name: micronaut-kubernetes

【讨论】:

    猜你喜欢
    • 2020-08-02
    • 1970-01-01
    • 2021-08-17
    • 2011-03-21
    • 2017-07-18
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2019-03-29
    相关资源
    最近更新 更多