添加全局异常处理
parent
edcd5c17d5
commit
22f78366d2
@ -1,49 +1,31 @@
|
||||
package com.ms.api;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.jinritemai.cloud.base.api.BaseRequest;
|
||||
import com.jinritemai.cloud.base.api.BaseResponse;
|
||||
import com.jinritemai.cloud.base.api.ExtensionService;
|
||||
import com.jinritemai.cloud.base.api.ExtensionServiceHandler;
|
||||
import com.ms.api.common.E;
|
||||
import com.ms.api.common.R;
|
||||
import com.ms.api.common.Ret;
|
||||
import com.ms.api.common.SPIBaseService;
|
||||
import com.ms.api.dto.ItemDTO;
|
||||
import com.ms.api.tool.DsJsonRequestTemplate;
|
||||
import com.ms.api.tool.SecurityTool;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
||||
@ExtensionService("testSpi")
|
||||
@Slf4j
|
||||
public class TestSpiService extends SPIBaseService implements ExtensionServiceHandler<ItemDTO, JSONObject> {
|
||||
@Autowired
|
||||
private DsJsonRequestTemplate dsJsonRequestTemplate;
|
||||
public class TestSpiService extends SPIBaseService implements ExtensionServiceHandler<ItemDTO, String> {
|
||||
|
||||
@Override
|
||||
public BaseResponse<JSONObject> handle(BaseRequest<ItemDTO> req) {
|
||||
initHandle(req);
|
||||
getAuthCode();
|
||||
HashMap<String, Object> params = new HashMap<>();
|
||||
params.put("productId",req.getData().getTitle());
|
||||
params.put("authCode",authCode);
|
||||
String res = null;
|
||||
JSONObject resObj = null;
|
||||
public BaseResponse<String> handle(BaseRequest<ItemDTO> req) {
|
||||
try{
|
||||
res = dsJsonRequestTemplate.execute("/micro_move/get_product_info", params);
|
||||
resObj = JSON.parseObject(res);
|
||||
log.info(res);
|
||||
} catch (Exception e) {
|
||||
initHandle(req);
|
||||
|
||||
// do something
|
||||
E.throwMSException("这里有全局异常抛出");
|
||||
return R.ok("这里执行不到");
|
||||
}catch (Throwable e){
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage());
|
||||
return R.ok(e.getMessage());
|
||||
}
|
||||
return R.ok(resObj);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
package com.ms.api.common;
|
||||
|
||||
/**
|
||||
* 异常管理
|
||||
*/
|
||||
public class E {
|
||||
public static void throwMSException(String msg) throws MSException {
|
||||
throw new MSException(msg);
|
||||
}
|
||||
|
||||
public static void throwMSException(String msg,Throwable e) throws MSException {
|
||||
throw new MSException(msg, e);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.ms.api.common;
|
||||
|
||||
/**
|
||||
* 自定义异常
|
||||
*/
|
||||
public class MSException extends RuntimeException {
|
||||
|
||||
public MSException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public MSException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue