Skip to content

Commit

Permalink
参数泛型限制fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yhyzgn committed Jun 5, 2020
1 parent e9fbcab commit a8b4f0d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.yhyzgn.http</groupId>
<artifactId>pigeon</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<name>Pigeon</name>
<description>Java http proxy.</description>
<url>/~https://github.com/yhyzgn/Pigeon</url>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.yhy.http.pigeon.offer;

import com.yhy.http.pigeon.Pigeon;
import com.yhy.http.pigeon.converter.Converter;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

/**
* author : 颜洪毅
* e-mail : yhyzgn@gmail.com
* time : 2020-06-05 1:15 下午
* version: 1.0.0
* desc : 内置字符串响应转换器
*/
public class StringResponseConverter extends GsonConverter {

@Nullable
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Pigeon pigeon) {
return new StringResponseBodyConverter();
}

private static class StringResponseBodyConverter implements Converter<ResponseBody, String> {
@Nullable
@Override
public String convert(ResponseBody from) throws IOException {
return from.string();
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/yhy/http/pigeon/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public static boolean hasUnresolvableType(@Nullable Type type) {
return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
}
if (type instanceof TypeVariable) {
return true;
return false;
}
if (type instanceof WildcardType) {
return true;
return false;
}
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or GenericArrayType, but <" + type + "> is of type " + className);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/pigeon/get/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.yhy.http.pigeon.annotation.method.GET;
import com.yhy.http.pigeon.annotation.param.Path;
import com.yhy.http.pigeon.annotation.param.Query;
import com.yhy.http.pigeon.common.Response;
import pigeon.interceptor.TestInterceptor;

import java.util.Map;
Expand Down Expand Up @@ -42,5 +43,5 @@ public interface Api {

@GET
@Interceptor(value = TestInterceptor.class, net = true)
Cat def(@Header Map<String, Object> header, @Query Map<String, Object> params);
String def(@Header Map<String, ?> header, @Query Map<String, Object> params);
}
7 changes: 4 additions & 3 deletions src/test/java/pigeon/get/ApiTester.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pigeon.get;

import com.yhy.http.pigeon.Pigeon;
import com.yhy.http.pigeon.offer.StringResponseConverter;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -16,7 +17,7 @@
public class ApiTester {

public static void main(String[] args) throws IOException {
Pigeon pigeon = new Pigeon.Builder().host("http://localhost:8080/api/get/cat").build();
Pigeon pigeon = new Pigeon.Builder().host("http://localhost:8080/api/get/cat").addConverterFactory(new StringResponseConverter()).build();
Api api = pigeon.create(Api.class);

// String test = api.test();
Expand Down Expand Up @@ -44,9 +45,9 @@ public static void main(String[] args) throws IOException {
params.put("age", "8");
params.put("remark", "附加信息");
// Cat cat = api.mp(header, params);
Cat cat = api.def(header, params);
String cat = api.def(header, params);
System.out.println(cat);
cat = api.def(header, params);
System.out.println(cat);
System.out.println(cat instanceof String);
}
}

0 comments on commit a8b4f0d

Please sign in to comment.