Skip to content

Commit 37245be

Browse files
authored
[#987]hessian support generics array (#988)
1 parent 854f430 commit 37245be

File tree

19 files changed

+556
-20
lines changed

19 files changed

+556
-20
lines changed

integration-tests/discovery-tests/common/src/main/java/com/huaweicloud/sample/hessian/HessianService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ public interface HessianService {
3232
@PostMapping(path = "nonSerializableModel", consumes = "x-application/hessian2",
3333
produces = "x-application/hessian2")
3434
NonSerializableModel nonSerializableModel(@RequestBody NonSerializableModel b);
35+
36+
@PostMapping(path = "nonSerializableModelArray", consumes = "x-application/hessian2",
37+
produces = "x-application/hessian2")
38+
NonSerializableModel[] nonSerializableModelArray(@RequestBody NonSerializableModel[] b);
3539
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.huaweicloud.sample.hessian;
19+
20+
import java.util.List;
21+
22+
import org.springframework.web.bind.annotation.PostMapping;
23+
import org.springframework.web.bind.annotation.RequestBody;
24+
25+
public interface IBaseService<T> {
26+
@PostMapping(path = "query", consumes = "x-application/hessian2",
27+
produces = "x-application/hessian2")
28+
T[] query(@RequestBody T[] input);
29+
30+
@PostMapping(path = "queryList", consumes = "x-application/hessian2",
31+
produces = "x-application/hessian2")
32+
List<T> queryList(@RequestBody List<T> input);
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
/*
18+
* Licensed to the Apache Software Foundation (ASF) under one or more
19+
* contributor license agreements. See the NOTICE file distributed with
20+
* this work for additional information regarding copyright ownership.
21+
* The ASF licenses this file to You under the Apache License, Version 2.0
22+
* (the "License"); you may not use this file except in compliance with
23+
* the License. You may obtain a copy of the License at
24+
*
25+
* http://www.apache.org/licenses/LICENSE-2.0
26+
*
27+
* Unless required by applicable law or agreed to in writing, software
28+
* distributed under the License is distributed on an "AS IS" BASIS,
29+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30+
* See the License for the specific language governing permissions and
31+
* limitations under the License.
32+
*/
33+
package com.huaweicloud.sample.hessian;
34+
35+
public interface IChildBaseService extends IBaseService<NonSerializableModel> {
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
3+
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
4+
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.huaweicloud.sample.hessian;
19+
20+
public interface IChildService extends IBaseService<NonSerializableModel> {
21+
}

integration-tests/discovery-tests/discovery-tests-client/src/test/java/com/huaweicloud/sample/OrderControllerIT.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,20 @@ public void testPojoModel() {
9797
assertThat(pojoModel.getName()).isEqualTo("hello");
9898
assertThat(pojoModel.getNum()).isEqualTo(2);
9999
}
100+
101+
@Test
102+
public void testHeaderWithJson() throws Exception {
103+
// feign need encode requests with value {}
104+
String v = "{\"name\": {\"age\": \"22\"}}";
105+
106+
URLCodec codec = new URLCodec("UTF-8");
107+
HttpHeaders headers = new HttpHeaders();
108+
headers.add("model", codec.encode(v));
109+
HttpEntity<Void> entity = new HttpEntity<>(headers);
110+
111+
String result = template.exchange(url + "/testHeaderWithJsonWrong", HttpMethod.POST, entity, String.class)
112+
.getBody();
113+
// Feign will keep response encoded and need request encode, this is quit inconvenient
114+
assertThat(result).isEqualTo(codec.encode(v));
115+
}
100116
}

integration-tests/discovery-tests/order-consumer/src/main/java/com/huaweicloud/sample/FeignService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
import org.springframework.web.bind.annotation.GetMapping;
2121
import org.springframework.web.bind.annotation.PostMapping;
2222
import org.springframework.web.bind.annotation.RequestBody;
23+
import org.springframework.web.bind.annotation.RequestHeader;
2324
import org.springframework.web.bind.annotation.RequestMapping;
2425
import org.springframework.web.bind.annotation.RequestParam;
2526
import org.springframework.web.bind.annotation.ResponseBody;
2627

28+
import feign.Headers;
29+
import feign.Param;
30+
2731
@FeignClient(name = "price")
2832
public interface FeignService {
2933
@PostMapping("/price")
@@ -64,4 +68,11 @@ public interface FeignService {
6468

6569
@PostMapping("/testPostModel")
6670
PojoModel testPostModel(@RequestBody PojoModel model);
71+
72+
@PostMapping("/testHeaderWithJson")
73+
public String testHeaderWithJsonWrong(@RequestHeader("model") String model);
74+
75+
@PostMapping("/testHeaderWithJson")
76+
@Headers("model: {model}")
77+
public String testHeaderWithJsonCorrect(@Param("model") String model);
6778
}

integration-tests/discovery-tests/order-consumer/src/main/java/com/huaweicloud/sample/OrderController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
package com.huaweicloud.sample;
1818

19+
import java.io.UnsupportedEncodingException;
20+
import java.net.URLEncoder;
21+
1922
import org.apache.commons.lang.StringUtils;
2023
import org.apache.servicecomb.service.center.client.model.Microservice;
2124
import org.apache.servicecomb.service.center.client.model.MicroserviceInstance;
@@ -26,6 +29,7 @@
2629
import org.springframework.web.bind.annotation.GetMapping;
2730
import org.springframework.web.bind.annotation.PostMapping;
2831
import org.springframework.web.bind.annotation.RequestBody;
32+
import org.springframework.web.bind.annotation.RequestHeader;
2933
import org.springframework.web.bind.annotation.RequestMapping;
3034
import org.springframework.web.bind.annotation.RequestMethod;
3135
import org.springframework.web.bind.annotation.RequestParam;
@@ -180,4 +184,22 @@ public PojoModel testPostModel(@RequestBody PojoModel model) {
180184
public PojoModel testPostModelFeign(@RequestBody PojoModel model) {
181185
return feignService.testPostModel(model);
182186
}
187+
188+
@PostMapping("/testHeaderWithJsonWrong")
189+
public String testHeaderWithJsonWrong(@RequestHeader String model) {
190+
return feignService.testHeaderWithJsonWrong(model);
191+
}
192+
193+
@PostMapping("/testHeaderWithJsonCorrect")
194+
public String testHeaderWithJsonCorrect(@RequestHeader String model) {
195+
return feignService.testHeaderWithJsonCorrect(model);
196+
}
197+
198+
public static String encodeHeader(String header) {
199+
try {
200+
return URLEncoder.encode(header, "UTF-8");
201+
} catch (UnsupportedEncodingException e) {
202+
return header;
203+
}
204+
}
183205
}

integration-tests/discovery-tests/order-consumer/src/main/java/com/huaweicloud/sample/hessian/HessianConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ public class HessianConfig {
2525
@FeignClient(name = "price", contextId = "hessianService", path = "/hessian")
2626
public interface HessianServiceExt extends HessianService {
2727
}
28+
29+
@FeignClient(name = "price", contextId = "childService", path = "/hessian/child")
30+
public interface IChildServiceExt extends IChildService {
31+
}
32+
33+
@FeignClient(name = "price", contextId = "childServiceBase", path = "/hessian/baseChild")
34+
public interface IChildBaseServiceExt extends IChildBaseService {
35+
}
2836
}

integration-tests/discovery-tests/order-consumer/src/main/java/com/huaweicloud/sample/hessian/HessianController.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package com.huaweicloud.sample.hessian;
1919

2020
import java.util.Arrays;
21+
import java.util.List;
2122

2223
import org.apache.commons.lang3.StringUtils;
24+
import org.checkerframework.checker.units.qual.A;
2325
import org.springframework.beans.factory.annotation.Autowired;
2426
import org.springframework.web.bind.annotation.RequestMapping;
2527
import org.springframework.web.bind.annotation.RestController;
@@ -30,16 +32,64 @@ public class HessianController {
3032
@Autowired
3133
private HessianService hessianService;
3234

35+
@Autowired
36+
private IChildService childService;
37+
38+
@Autowired
39+
private IChildBaseService childBaseService;
40+
3341
@RequestMapping("/testHessian")
3442
public String testHessian() {
3543
testBase();
3644
testChildBase();
3745
testGenericBase();
3846
testGenericChildBase();
3947
testNonSerializableModel();
48+
testNonSerializableModelArray();
49+
testInterfaceInheritanceList();
50+
testInterfaceInheritance();
51+
testProviderImplementsBase();
4052
return "success";
4153
}
4254

55+
private void testProviderImplementsBase() {
56+
NonSerializableModel model = new NonSerializableModel();
57+
model.setAge("age");
58+
59+
List<NonSerializableModel> resultList = childBaseService.queryList(Arrays.asList(model));
60+
check(1 + "", resultList.size() + "", "wrong NonSerializableModel");
61+
check("age", resultList.get(0).getAge(), "wrong NonSerializableModel");
62+
63+
// TODO : this test case not supported now, should support later
64+
// NonSerializableModel[] result = childBaseService.query(new NonSerializableModel[] {model});
65+
// check(1 + "", result.length + "", "wrong NonSerializableModel");
66+
// check("age", result[0].getAge(), "wrong NonSerializableModel");
67+
}
68+
69+
private void testInterfaceInheritance() {
70+
NonSerializableModel model = new NonSerializableModel();
71+
model.setAge("age");
72+
NonSerializableModel[] result = childService.query(new NonSerializableModel[] {model});
73+
check(1 + "", result.length + "", "wrong NonSerializableModel");
74+
check("age", result[0].getAge(), "wrong NonSerializableModel");
75+
}
76+
77+
private void testInterfaceInheritanceList() {
78+
NonSerializableModel model = new NonSerializableModel();
79+
model.setAge("age");
80+
List<NonSerializableModel> result = childService.queryList(Arrays.asList(model));
81+
check(1 + "", result.size() + "", "wrong NonSerializableModel");
82+
check("age", result.get(0).getAge(), "wrong NonSerializableModel");
83+
}
84+
85+
private void testNonSerializableModelArray() {
86+
NonSerializableModel model = new NonSerializableModel();
87+
model.setAge("age");
88+
NonSerializableModel[] result = hessianService.nonSerializableModelArray(new NonSerializableModel[] {model});
89+
check(1 + "", result.length + "", "wrong NonSerializableModel");
90+
check("age", result[0].getAge(), "wrong NonSerializableModel");
91+
}
92+
4393
private void testNonSerializableModel() {
4494
NonSerializableModel model = new NonSerializableModel();
4595
model.setAge("age");

integration-tests/discovery-tests/price-provider/src/main/java/com/huaweicloud/sample/PriceController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.web.bind.annotation.PostMapping;
2626
import org.springframework.web.bind.annotation.RequestBody;
27+
import org.springframework.web.bind.annotation.RequestHeader;
2728
import org.springframework.web.bind.annotation.RequestMapping;
2829
import org.springframework.web.bind.annotation.RequestParam;
2930
import org.springframework.web.bind.annotation.RestController;
@@ -119,4 +120,9 @@ public String priceBalance(@RequestParam("id") String id) {
119120
public PojoModel testPostModel(@RequestBody PojoModel model) {
120121
return model;
121122
}
123+
124+
@PostMapping("/testHeaderWithJson")
125+
public String testHeaderWithJson(@RequestHeader("model") String model) {
126+
return model;
127+
}
122128
}

0 commit comments

Comments
 (0)