Skip to content

Commit

Permalink
feat(内核): 支持 Get 请求
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Jan 16, 2025
1 parent 5fe0202 commit 196a0d9
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 12 deletions.
9 changes: 9 additions & 0 deletions docs/content/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: 首页

config:
sidebar: false
toc: false
---

LightCall 是一个简单、直观且功能强大的服务调用框架。通过声明式的方式,让开发者专注于业务逻辑而不是底层的 HTTP 调用细节。
30 changes: 30 additions & 0 deletions docs/content/usage/get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Get
---

LightCall 提供了 HTTP GET 请求的支持。

!!! danger "注意"

LightCall 需要在方法上添加 `@Get` 注解来标识该方法是一个 HTTP GET 请求。并且添加 `@Get` 注解的类必须是一个接口。

!!!

我们使用的模拟数据是,他的代码可以在 [这里](https://github.com/devliveorg/lightcall/blob/dev/src/test/java/org/devlive/lightcall/example/PostService.java "PostService" "_blank") 查看。

```java
public interface PostService
{}
```

### 用法

```java

@Get("/posts")
List<Post> getPosts();
```

### 返回值

返回值类型为 List<Post>。
4 changes: 2 additions & 2 deletions docs/pageforge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ site:
description: LightCall 的设计目标是提供一个简单、直观且功能强大的服务调用框架。通过声明式的方式,让开发者专注于业务逻辑而不是底层的 HTTP 调用细节。

nav:
- link: /
text: 首页
- 文档:
- /usage/get
27 changes: 17 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@

<!-- 依赖版本 -->
<okhttp.version>4.12.0</okhttp.version>
<gson.version>2.10.1</gson.version>
<slf4j.version>2.0.11</slf4j.version>
<jackson.version>2.16.1</jackson.version>
<logback.version>1.5.6</logback.version>
<junit.version>5.10.1</junit.version>
<mockito.version>5.10.0</mockito.version>
<lombok.version>1.18.34</lombok.version>

<!-- 插件版本 -->
<checkstyle.version>10.12.5</checkstyle.version>
Expand All @@ -62,14 +63,9 @@
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -89,6 +85,17 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/devlive/lightcall/LightCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.devlive.lightcall;

import org.devlive.lightcall.config.LightCallConfig;
import org.devlive.lightcall.proxy.LightCallProxy;

import java.lang.reflect.Proxy;

public class LightCall
{
@SuppressWarnings("unchecked")
public static <T> T create(Class<T> serviceClass, LightCallConfig config)
{
validateServiceInterface(serviceClass);

return (T) Proxy.newProxyInstance(
serviceClass.getClassLoader(),
new Class[] {serviceClass},
new LightCallProxy(config)
);
}

private static void validateServiceInterface(Class<?> service)
{
if (!service.isInterface()) {
throw new IllegalArgumentException("API declarations must be interfaces.");
}
if (service.getInterfaces().length > 0) {
throw new IllegalArgumentException("API interfaces must not extend other interfaces.");
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/devlive/lightcall/RequestException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.devlive.lightcall;

public class RequestException
extends RuntimeException
{
public RequestException(String message)
{
super(message);
}

public RequestException(String message, Throwable cause)
{
super(message, cause);
}
}
15 changes: 15 additions & 0 deletions src/main/java/org/devlive/lightcall/annotation/Get.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.devlive.lightcall.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Get
{
String value() default "";
}
17 changes: 17 additions & 0 deletions src/main/java/org/devlive/lightcall/annotation/RequestParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.devlive.lightcall.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestParam
{
String value() default "";

boolean required() default true;
}
42 changes: 42 additions & 0 deletions src/main/java/org/devlive/lightcall/config/LightCallConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.devlive.lightcall.config;

import lombok.Data;

@Data
public class LightCallConfig
{
private String baseUrl;
private int connectTimeout;
private int readTimeout;

private LightCallConfig(String baseUrl)
{
this.baseUrl = baseUrl;
}

public static LightCallConfig create(String baseUrl)
{
return new LightCallConfig(baseUrl)
.connectTimeout(5000)
.readTimeout(5000);
}

public static LightCallConfig create(String baseUrl, int connectTimeout, int readTimeout)
{
return LightCallConfig.create(baseUrl)
.connectTimeout(connectTimeout)
.readTimeout(readTimeout);
}

public LightCallConfig connectTimeout(int connectTimeout)
{
this.connectTimeout = connectTimeout;
return this;
}

public LightCallConfig readTimeout(int readTimeout)
{
this.readTimeout = readTimeout;
return this;
}
}
Loading

0 comments on commit 196a0d9

Please sign in to comment.