Commit cf01b1db by fanjiaxin

代码初始化

parent a8df588c
Pipeline #71455 passed with stages
in 1 minute 12 seconds
package com.netease.yanxuan.wx.store.sharer.common.exception;
/**
* 微信业务-异常类
*/
public class WeChatException extends RuntimeException {
private static final long serialVersionUID = -7960867947542194198L;
public WeChatException() {
super();
}
public WeChatException(String message) {
super(message);
}
public WeChatException(String message, Throwable cause) {
super(message, cause);
}
public WeChatException(Throwable cause) {
super(cause);
}
public WeChatException(Object obj) {
super();
}
public WeChatException(String message, Object obj) {
super(message);
}
public WeChatException(String message, Throwable cause, Object obj) {
super(message, cause);
}
public WeChatException(Throwable cause, Object obj) {
super(cause);
}
}
......@@ -5,6 +5,7 @@ import com.netease.yanxuan.wx.store.sharer.common.constant.HttpStatusConstant;
import com.netease.yanxuan.wx.store.sharer.common.core.Result;
import com.netease.yanxuan.wx.store.sharer.common.exception.BizException;
import com.netease.yanxuan.wx.store.sharer.common.exception.NoAuthException;
import com.netease.yanxuan.wx.store.sharer.common.exception.WeChatException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
......@@ -39,10 +40,20 @@ public class GlobalExceptionHandler {
}
/**
* 业务异常-微信
*/
@ExceptionHandler(WeChatException.class)
public Result<Void> handleWeChatException(WeChatException e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("请求地址'{}',微信业务异常.", requestURI, e);
return Result.fail(e);
}
/**
* 业务异常
*/
@ExceptionHandler(BizException.class)
public Result<Void> handleYiCaiServiceException(Exception e, HttpServletRequest request) {
public Result<Void> handleBizException(Exception e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生业务异常.", requestURI, e);
return Result.fail(e);
......
......@@ -14,8 +14,7 @@ public interface HttpRequestClient {
* @param url 请求地址
* @param method 请求类型 (GET/POST)
* @param params 请求参数 (GET 对应查询参数,POST 对应表单/JSON 参数)
* @param resType 返回数据类型
* @return 响应结果
*/
<T, R> R execute(String url, HttpMethod method, T params, Class<R> resType);
<T> String execute(String url, HttpMethod method, T params);
}
......@@ -10,6 +10,7 @@ import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.Map;
import java.util.Objects;
/**
* @Description RestTemplateClient
......@@ -23,33 +24,35 @@ public class RestTemplateClient implements HttpRequestClient {
private final RestTemplate restTemplate;
@Override
public <T, R> R execute(String url, HttpMethod method, T params, Class<R> resType) {
public <T> String execute(String url, HttpMethod method, T params) {
log.info("RestTemplateClient execute url:{},method:{},params:{}", url, method, JSON.toJSONString(params));
if (method == HttpMethod.GET) {
return handleGetRequest(url, params, resType);
return handleGetRequest(url, params);
} else {
return handlePostRequest(url, params, resType);
return handlePostRequest(url, params);
}
}
private <T, R> R handleGetRequest(String url, T params, Class<R> resType) {
private <T> String handleGetRequest(String url, T params) {
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
Map<String, Object> paramsMap = JSON.parseObject(JSON.toJSONString(params), Map.class);
paramsMap.forEach(builder::queryParam);
if (Objects.nonNull(params)) {
Map<String, Object> paramsMap = JSON.parseObject(JSON.toJSONString(params), Map.class);
paramsMap.forEach(builder::queryParam);
}
URI uri = builder.build().toUri();
log.info("RestTemplateClient handleGetRequest execute uri:{}", uri);
ResponseEntity<R> response = restTemplate.getForEntity(url, resType, paramsMap);
log.info("RestTemplateClient handleGetRequest execute response:{}", JSON.toJSONString(response));
return response.getBody();
String result = restTemplate.getForObject(uri, String.class);
log.info("RestTemplateClient handleGetRequest execute response:{}", result);
return result;
}
private <T, R> R handlePostRequest(String url, T params, Class<R> resType) {
private <T> String handlePostRequest(String url, T params) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<T> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<R> response = restTemplate.postForEntity(url, requestEntity, resType);
log.info("RestTemplateClient handlePostRequest execute response:{}", JSON.toJSONString(response));
return response.getBody();
String result = restTemplate.postForObject(url, requestEntity, String.class);
log.info("RestTemplateClient handlePostRequest execute response:{}", result);
return result;
}
}
package com.netease.yanxuan.wx.store.sharer.integration.handler;
import com.netease.yanxuan.wx.store.sharer.common.exception.BizException;
import com.alibaba.fastjson.JSON;
import com.netease.yanxuan.wx.store.sharer.common.exception.WeChatException;
import com.netease.yanxuan.wx.store.sharer.common.handler.RestTemplateClient;
import com.netease.yanxuan.wx.store.sharer.integration.constant.WeChatApi;
import com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatCoreVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
......@@ -21,9 +23,16 @@ public class WeChatRestTemplateHandler {
private final RestTemplateClient restTemplateClient;
public <T, R extends WeChatCoreVO> R execute(String url, HttpMethod method, T params, Class<R> resType) {
R result = restTemplateClient.execute(url, method, params, resType);
if (!WeChatApi.WECHAT_REQUEST_SUCCESS_CODE.equals(result.getErrcode())) {
throw new BizException("调用微信接口失败," + result.getErrmsg());
R result;
try {
String resultJson = restTemplateClient.execute(url, method, params);
result = JSON.parseObject(resultJson, resType);
} catch (Exception e) {
throw new WeChatException(e);
}
if (StringUtils.isNotBlank(result.getErrcode())
&& !WeChatApi.WECHAT_REQUEST_SUCCESS_CODE.equals(result.getErrcode())) {
throw new WeChatException("调用微信接口失败," + result.getErrmsg());
}
return result;
}
......
......@@ -6,11 +6,15 @@
*/
package com.netease.yanxuan.wx.store.sharer.web.controller;
import com.alibaba.fastjson.JSON;
import com.netease.yanxuan.wx.store.sharer.common.core.Result;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatAccessTokenRequest;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatUserInfoRequest;
import com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatAccessTokenVO;
import com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatUserInfoVO;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -22,14 +26,18 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {
private static final Logger log = LoggerFactory.getLogger(TestController.class);
private final WeChatAccessTokenRequest weChatAccessTokenRequest;
private final WeChatUserInfoRequest weChatUserInfoRequest;
@RequestMapping("/token")
public Result<WeChatUserInfoVO> token() {
WeChatUserInfoVO handle = weChatUserInfoRequest.handle("0f11uE0w3hIUx43hre0w3NwgHb31uE0Z");
WeChatAccessTokenVO accessToken = weChatAccessTokenRequest.handle();
log.info("accessToken={}", JSON.toJSONString(accessToken));
WeChatUserInfoVO userInfo = weChatUserInfoRequest.handle("0f11uE0w3hIUx43hre0w3NwgHb31uE0Z");
log.info("userInfo={}", JSON.toJSONString(userInfo));
return Result.ok(handle);
return Result.ok();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment