-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
415 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
title: 首页 | ||
|
||
config: | ||
sidebar: false | ||
toc: false | ||
--- | ||
|
||
LightCall 是一个简单、直观且功能强大的服务调用框架。通过声明式的方式,让开发者专注于业务逻辑而不是底层的 HTTP 调用细节。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/main/java/org/devlive/lightcall/annotation/RequestParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/main/java/org/devlive/lightcall/config/LightCallConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.