Skip to content

Commit 56cca62

Browse files
committed
添加异常类
1 parent 35a12ac commit 56cca62

4 files changed

Lines changed: 47 additions & 21 deletions

File tree

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java

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

44
import com.alibaba.fastjson.JSON;
55
import com.alibaba.fastjson.parser.ParserConfig;
6+
import io.kimmking.rpcfx.exception.RpcfxException;
67
import io.kimmking.rpcfx.param.RpcfxRequest;
78
import io.kimmking.rpcfx.param.RpcfxResponse;
89
import okhttp3.MediaType;
@@ -18,7 +19,11 @@
1819
public final class Rpcfx {
1920

2021
static {
21-
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
22+
ParserConfig config = ParserConfig.getGlobalInstance();
23+
config.addAccept("io.kimmking");
24+
config.setAutoTypeSupport(true);
25+
26+
//ParserConfig.getGlobalInstance().addAccept("io.kimmking");
2227
}
2328

2429
public static <T> T create(final Class<T> serviceClass, final String url) {
@@ -51,11 +56,14 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
5156
request.setParams(params);
5257

5358
RpcfxResponse response = post(request, url);
54-
59+
if (response.isStatus()) {
60+
return response.getResult();
61+
} else {
62+
RpcfxException e = (RpcfxException) response.getException();
63+
return null;
64+
}
5565
// 这里判断response.status,处理异常
5666
// 考虑封装一个全局的RpcfxException
57-
58-
return JSON.parse(response.getResult().toString());
5967
}
6068

6169
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.kimmking.rpcfx.exception;
2+
3+
public class RpcfxException extends Exception {
4+
5+
public RpcfxException() {
6+
super();
7+
}
8+
9+
public RpcfxException(Throwable throwable) {
10+
super(throwable);
11+
}
12+
13+
public RpcfxException(String exception) {
14+
super(exception);
15+
}
16+
17+
}

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.alibaba.fastjson.JSON;
44
import com.alibaba.fastjson.serializer.SerializerFeature;
55
import io.kimmking.rpcfx.api.RpcfxReflectionResolver;
6+
import io.kimmking.rpcfx.exception.RpcfxException;
67
import io.kimmking.rpcfx.param.RpcfxRequest;
78
import io.kimmking.rpcfx.api.RpcfxResolver;
89
import io.kimmking.rpcfx.param.RpcfxResponse;
@@ -13,24 +14,24 @@
1314

1415
public class RpcfxInvoker {
1516

16-
/* private RpcfxResolver resolver;
17+
private RpcfxResolver resolver;
1718

1819
public RpcfxInvoker(RpcfxResolver resolver){
1920
this.resolver = resolver;
20-
}*/
21-
private RpcfxReflectionResolver reflectionResolver;
22-
23-
public RpcfxInvoker(RpcfxReflectionResolver reflectionResolver) {
24-
this.reflectionResolver = reflectionResolver;
2521
}
22+
//private RpcfxReflectionResolver reflectionResolver;
23+
24+
// public RpcfxInvoker(RpcfxReflectionResolver reflectionResolver) {
25+
// this.reflectionResolver = reflectionResolver;
26+
// }
2627

2728
public RpcfxResponse invoke(RpcfxRequest request) {
2829
RpcfxResponse response = new RpcfxResponse();
2930
String serviceClass = request.getServiceClass();
3031

3132
// 作业1:改成泛型和反射
32-
//Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass);
33-
Object service = reflectionResolver.resolve(serviceClass);
33+
Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass);
34+
//Object service = reflectionResolver.resolve(serviceClass);
3435

3536
try {
3637
Method method = resolveMethodFromClass(service.getClass(), request.getMethod());
@@ -46,7 +47,7 @@ public RpcfxResponse invoke(RpcfxRequest request) {
4647
// 2.封装一个统一的RpcfxException
4748
// 客户端也需要判断异常
4849
e.printStackTrace();
49-
response.setException(e);
50+
response.setException(new RpcfxException(e));
5051
response.setStatus(false);
5152
return response;
5253
}

07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ public RpcfxResponse invoke(@RequestBody RpcfxRequest request) {
3232
}
3333

3434
@Bean
35-
public RpcfxInvoker createInvoker(@Autowired RpcfxReflectionResolver resolver){
35+
public RpcfxInvoker createInvoker(@Autowired RpcfxResolver resolver){
3636
return new RpcfxInvoker(resolver);
3737
}
3838

39-
@Bean
40-
public RpcfxReflectionResolver createReflectionResolver() {
41-
return new ReflectionResolver();
42-
}
43-
4439
// @Bean
45-
// public RpcfxResolver createResolver(){
46-
// return new DemoResolver();
40+
// public RpcfxReflectionResolver createReflectionResolver() {
41+
// return new ReflectionResolver();
4742
// }
4843

44+
@Bean
45+
public RpcfxResolver createResolver(){
46+
return new DemoResolver();
47+
}
48+
4949
// 能否去掉name
5050
//
5151
@Bean(name = "io.kimmking.rpcfx.demo.api.UserService")

0 commit comments

Comments
 (0)