Solon v2.7.5

一、Hello Nami

</> markdown

这是个 Helloworld 示例,通过调用 github-api 接口获取数据。可以在任何应用开发框架下有效。

1、添加依赖

<dependencies>
    <dependency>
        <groupId>org.noear</groupId>
        <artifactId>nami.coder.snack3</artifactId>
    </dependency>

    <dependency>
        <groupId>org.noear</groupId>
        <artifactId>nami.channel.http.okhttp</artifactId>
    </dependency>
</dependencies>

2、编写代码

public class MainTest {
    public static void main(String... args) {
        GitHub github = Nami.builder()
                .decoder(new SnackDecoder())
                .channel(new HttpChannel())
                .upstream(() -> "https://api.github.com")
                .create(GitHub.class);

        // Fetch and print a list of the contributors to this library.
        List<Contributor> contributors = github.contributors("OpenFeign", "feign");
        for (Contributor contributor : contributors) {
            System.out.println(contributor.login + " (" + contributor.contributions + ")");
        }
    }
}

public interface GitHub {
    @NamiMapping("GET /repos/{owner}/{repo}/contributors")
    List<Contributor> contributors(String owner, String repo);

    @NamiMapping("POST /repos/{owner}/{repo}/issues")
    void createIssue(Issue issue, String owner, String repo);
}

public class Contributor {
    public String login;
    public int contributions;
}

public class Issue {
    public String title;
    public String body;
    public List<String> assignees;
    public int milestone;
    public List<String> labels;
}