Skip to content

Commit 21fba83

Browse files
committed
ResultBeanResponseProcessor: throw UnexpectedResultException when response body without expected code; fix an infinite loop bug when the ResultBean object has super class;
1 parent 9e64a98 commit 21fba83

8 files changed

Lines changed: 72 additions & 13 deletions

File tree

README-en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ The only thing we need to do is defining the interface.
3131
<dependency>
3232
<groupId>com.github.dadiyang</groupId>
3333
<artifactId>http-api-invoker</artifactId>
34-
<version>1.2.0</version>
34+
<version>1.2.1</version>
3535
</dependency>
3636
```
3737

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
4. 若使用 Spring ,则可以使用 Autowired 自动注入接口的实现
1717
5. 完善的文档用例和单元测试
1818
6. 支持 Mock
19-
7. JDK6+(注:1.2.0版本之前JDK8
19+
7. JDK6+(注:1.2.0版本后支持JDK6,之前的版本必须JDK8
2020

2121
# 技术栈
2222

@@ -33,7 +33,7 @@
3333
<dependency>
3434
<groupId>com.github.dadiyang</groupId>
3535
<artifactId>http-api-invoker</artifactId>
36-
<version>1.2.0</version>
36+
<version>1.2.1</version>
3737
</dependency>
3838
```
3939

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.dadiyang</groupId>
88
<artifactId>http-api-invoker</artifactId>
9-
<version>1.2.0</version>
9+
<version>1.2.1</version>
1010
<packaging>jar</packaging>
1111
<parent>
1212
<groupId>org.sonatype.oss</groupId>
@@ -34,8 +34,8 @@
3434
</developers>
3535
<properties>
3636
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37-
<spring.version>4.3.5.RELEASE</spring.version>
38-
<fastjson.version>1.2.54</fastjson.version>
37+
<spring.version>4.3.24.RELEASE</spring.version>
38+
<fastjson.version>1.2.58</fastjson.version>
3939
<slf4j.version>1.7.21</slf4j.version>
4040
<jsoup.version>1.11.2</jsoup.version>
4141
<jacoco.version>0.7.5.201505241946</jacoco.version>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.dadiyang.httpinvoker.exception;
2+
3+
/**
4+
* Signals that a request has received an unexpected result.
5+
*
6+
* @author dadiyang
7+
* @since 2019-07-09
8+
*/
9+
public class UnexpectedResultException extends IllegalStateException {
10+
public UnexpectedResultException() {
11+
}
12+
13+
public UnexpectedResultException(String s) {
14+
super(s);
15+
}
16+
17+
public UnexpectedResultException(String message, Throwable cause) {
18+
super(message, cause);
19+
}
20+
21+
public UnexpectedResultException(Throwable cause) {
22+
super(cause);
23+
}
24+
}

src/main/java/com/github/dadiyang/httpinvoker/requestor/ResultBeanResponseProcessor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.alibaba.fastjson.JSONObject;
55
import com.github.dadiyang.httpinvoker.annotation.ExpectedCode;
66
import com.github.dadiyang.httpinvoker.annotation.HttpReq;
7+
import com.github.dadiyang.httpinvoker.exception.UnexpectedResultException;
78
import com.github.dadiyang.httpinvoker.util.ObjectUtils;
89
import com.github.dadiyang.httpinvoker.util.ParamUtils;
910
import org.slf4j.Logger;
@@ -34,7 +35,7 @@ public class ResultBeanResponseProcessor implements ResponseProcessor {
3435
private Map<Class<?>, Boolean> isResultBeanCache = new ConcurrentHashMap<Class<?>, Boolean>();
3536

3637
@Override
37-
public Object process(HttpResponse response, Method method) {
38+
public Object process(HttpResponse response, Method method) throws UnexpectedResultException {
3839
// 对返回值进行解析,code 为 0,则返回反序列化 data 的值,否则抛出异常
3940
String rs = response.getBody();
4041
// 以下几种情况下,无需解析响应
@@ -72,9 +73,8 @@ public Object process(HttpResponse response, Method method) {
7273
String uri = req != null ? req.value() : method.getName();
7374
// 兼容两种错误信息的写法
7475
String errMsg = obj.containsKey(MESSAGE) ? obj.getString(MESSAGE) : obj.getString(MSG);
75-
String msg = "请求api失败, uri: " + uri + ", 错误信息: " + errMsg;
76-
log.warn(msg);
77-
throw new IllegalStateException(msg);
76+
log.warn("请求api失败, uri: " + uri + ", 错误信息: " + errMsg);
77+
throw new UnexpectedResultException(errMsg);
7878
}
7979
}
8080

@@ -149,9 +149,9 @@ private Field[] getDeclaredFields(Class<?> returnType) {
149149
} else {
150150
List<Field> fields = new ArrayList<Field>();
151151
Class<?> type = returnType;
152-
while (type != Object.class && !type.isInterface()) {
152+
while (type != null && type != Object.class && !type.isInterface()) {
153153
fields.addAll(Arrays.asList(type.getDeclaredFields()));
154-
type = returnType.getSuperclass();
154+
type = type.getSuperclass();
155155
}
156156
return fields.toArray(new Field[]{});
157157
}

src/main/java/com/github/dadiyang/httpinvoker/util/ParamUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public class ParamUtils {
2727
private static final String FORM_KEY = "formKey";
2828
private static final List<Class<?>> BASIC_TYPE = Arrays.asList(Byte.class, Short.class,
2929
Integer.class, Long.class, Float.class, Double.class, Character.class,
30-
Boolean.class, String.class, Void.class);
30+
Boolean.class, String.class, Void.class, Date.class);
31+
/**
32+
* for JDK6/7 compatibility
33+
*/
34+
private static final List<String> BASIC_TYPE_NAME = Arrays.asList("java.time.LocalDate", "java.time.LocalDateTime");
3135

3236
private ParamUtils() {
3337
throw new UnsupportedOperationException("utils should not be initialized!");
@@ -43,6 +47,10 @@ public static boolean isBasicType(Class<?> clz) {
4347
if (clz == null) {
4448
return false;
4549
}
50+
// for JDK6/7 compatibility
51+
if (BASIC_TYPE_NAME.contains(clz.getName())) {
52+
return true;
53+
}
4654
return clz.isPrimitive() || BASIC_TYPE.contains(clz);
4755
}
4856

src/test/java/com/github/dadiyang/httpinvoker/interfaces/CityService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.github.dadiyang.httpinvoker.requestor.Status;
1212

1313
import java.io.InputStream;
14+
import java.util.Date;
1415
import java.util.List;
1516
import java.util.Map;
1617

@@ -197,4 +198,10 @@ String upload(@Param("fileName") String fileName,
197198

198199
@HttpReq(value = "/invalid", method = "xxx")
199200
void invalidMethod();
201+
202+
/**
203+
* 测试 date 类型参数及返回值
204+
*/
205+
@HttpReq(value = "/date", method = ReqMethod.POST)
206+
Date getDate(@Param("date") Date date);
200207
}

src/test/java/com/github/dadiyang/httpinvoker/interfaces/CityServiceTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,24 @@ public void getCityByComplicatedInfo() throws UnsupportedEncodingException {
553553
City rs = cityService.getCityByComplicatedInfo(info);
554554
assertEquals(city, rs);
555555
}
556+
557+
@Test
558+
public void getDate() throws UnsupportedEncodingException {
559+
String uri = "/city/date";
560+
Map<String, Object> param = new HashMap<String, Object>();
561+
Date now = new Date();
562+
param.put("date", now);
563+
wireMockRule.stubFor(post(urlPathEqualTo(uri))
564+
.withRequestBody(equalToJson(JSON.toJSONString(param)))
565+
.willReturn(aResponse().withBody(JSON.toJSONString(now))));
566+
Date date = cityService.getDate(now);
567+
assertEquals(date.getTime(), now.getTime());
568+
569+
ResultBean<Date> resultBean = new ResultBean<Date>(0, now);
570+
wireMockRule.stubFor(post(urlPathEqualTo(uri))
571+
.withRequestBody(equalToJson(JSON.toJSONString(param)))
572+
.willReturn(aResponse().withBody(JSON.toJSONString(resultBean))));
573+
date = cityServiceWithResultBeanResponseProcessor.getDate(now);
574+
assertEquals(date.getTime(), now.getTime());
575+
}
556576
}

0 commit comments

Comments
 (0)