Commit 7723a017 by tyfanchz

APP端功能开发

1 parent 6840cbd8
package com.glsx.api.controller;
import com.alibaba.fastjson.JSONObject;
import com.glsx.api.remote.ProductTypeFeignClient;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-17
*/
@Slf4j
@RestController
@RequestMapping("/productType")
public class ProductTypeController {
@Autowired
private ProductTypeFeignClient productTypeFeignClient;
/**
* 获取结构化的产品类型。
*
* @return 结果
*/
@RequestMapping(value = "/getTypeAndCategory", method = {RequestMethod.GET, RequestMethod.POST})
public R getProductTypeTree() {
log.info("$$$$ /productType/getTypeAndCategory...");
JSONObject result = productTypeFeignClient.getTypeAndCategory();
return R.ok().data(result);
}
}
package com.glsx.api.exception;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-16
*/
public class BusinessException extends RuntimeException {
private Integer code;
private String message;
public BusinessException() {}
public BusinessException(ResultEnum resultEnum) {
this.code = resultEnum.getCode();
this.message = resultEnum.getMessage();
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.glsx.api.exception;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-16
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler({BusinessException.class})
public R handleBusinessException(BusinessException e) {
log.error("!!!! 业务异常", e);
return R.error(e.getCode(), e.getMessage());
}
@ExceptionHandler({ServletRequestBindingException.class, MethodArgumentNotValidException.class,
BindException.class})
public R handleBindException(Exception e) {
log.error("!!!! 参数错误", e);
return R.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
}
@ExceptionHandler({HystrixRuntimeException.class})
public R handleHystrixException(HystrixRuntimeException e) {
Throwable cause = e.getCause();
if (cause instanceof FeignException) {
FeignException feignException = (FeignException) cause;
return this.handleFeignException(feignException);
} else {
log.error("!!!! Hystrix熔断错误", e);
return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
}
}
@ExceptionHandler({FeignException.class})
public R handleFeignException(FeignException e) {
log.error("!!!! 远程服务错误", e);
JSONObject respObj = JSON.parseObject(e.contentUTF8());
String respMessage = respObj.getString("message");
int code = ResultEnum.REMOTE_SERVICE_ERROR.getCode();
String message = StringUtils.isBlank(respMessage)
? ResultEnum.REMOTE_SERVICE_ERROR.getMessage() : respMessage;
return R.error(code, message);
}
@ExceptionHandler(Exception.class)
public R handleException(Exception e) {
log.error("!!!! 未知异常", e);
return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
}
}
package com.glsx.api.exception;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-16
*/
public enum ResultEnum {
SUCCESS(200, "成功"),
ERROR(500, "系统异常"),
PARAM_ERROR(1001, "参数错误"),
ID_NOT_FOUND(1002, "ID不存在"),
PRODUCT_NOT_FOUND(2001, "产品ID未找到"),
PRODUCT_NAME_DUPLICATE(2002, "产品名称重复"),
PRODUCT_TYPE_NOT_FOUND(3001, "产品类型ID未找到"),
REMOTE_SERVICE_ERROR(9001, "服务调用错误"),
;
private final Integer code;
private final String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}
package com.glsx.api.filter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
/**
* <p>Description: 跨域配置
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-18
*/
@Component
public class CorsFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
StringBuffer headersSb = new StringBuffer();
Enumeration<String> headerNames = httpRequest.getHeaderNames();
while (headerNames.hasMoreElements()) {
headersSb.append(headerNames.nextElement()).append(", ");
}
if (!headersSb.toString().toLowerCase().contains("authorization")) {
headersSb.append("authorization");
}
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS,
headersSb.toString().replaceAll(", +$", ""));
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,
httpRequest.getHeader(HttpHeaders.ORIGIN));
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS,
httpRequest.getMethod());
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "18000L");
chain.doFilter(request, response);
}
}
package com.glsx.api.remote;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-17
*/
@FeignClient(name = "${feign.server.name}",url = "${feign.server.url}")
public interface ProductTypeFeignClient {
@RequestMapping(value = "/productType/getTypeAndCategory", method = {RequestMethod.GET})
@ResponseBody
JSONObject getTypeAndCategory();
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!