spring cloud alibaba开发笔记一(项目初始化)

初始化项目

首先创建一个父项目。父项目的pom如图所示:



    4.0.0

    org.example
    e-commerce-springcloud
    1.0-SNAPSHOT

    pom

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
    

    
        
        Hoxton.SR8
        
        2.2.4.RELEASE
    

    
        
        
            org.projectlombok
            lombok
            1.16.18
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.apache.commons
            commons-lang3
            3.11
        
        
            org.apache.commons
            commons-collections4
            4.4
        
        
            cn.hutool
            hutool-all
            5.6.0
        
        
        
            io.jsonwebtoken
            jjwt-api
            0.10.5
        
        
            io.jsonwebtoken
            jjwt-impl
            0.10.5
            runtime
        
        
            io.jsonwebtoken
            jjwt-jackson
            0.10.5
            runtime
        
        
            com.alibaba
            fastjson
            1.2.47
        
    

    
    
        
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            

            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                ${spring-cloud-alibaba.version}
                pom
                import
            
        
    

    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
    

完成通用模块的编写:

通用模块pom文件示例:



    
        e-commerce-springcloud
        com.taluohui.ecommerce
        1.0-SNAPSHOT
    
    4.0.0

    e-commerce-common
    1.0-SNAPSHOT
    jar

    e-commerce-common
    通用模块

添加package-info占位文件

统一通用响应对象CommonResponse

创建vo包,并添加统一通用响应对象CommonResponse

/**
 * 

通用响应对象定义

* { * “code”: 0, * "message": "", * "data": {} * } */ @Data @NoArgsConstructor @AllArgsConstructor public class CommonResponse implements Serializable { /** 错误码 */ private Integer code; /** 错误消息 */ private String message; /** 泛型响应数据 */ private T data; public CommonResponse(Integer code, String message) { this.code = code; this.message = message; } }

通用配置模块初始化:

通用配置模块pom文件示例:



    
        e-commerce-springcloud
        com.taluohui.ecommerce
        1.0-SNAPSHOT
    
    4.0.0

    e-commerce-mvc-config
    1.0-SNAPSHOT
    jar

    
    e-commerce-mvc-config
    通用配置模块

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.taluohui.ecommerce
            e-commerce-common
            1.0-SNAPSHOT
        
    

添加package-info占位文件

实现统一响应

/**
 * 

实现统一响应

*/ @RestControllerAdvice(value = "com.taluohui.ecommerce") public class CommonResponseDataAdvice implements ResponseBodyAdvice { /** *

判断是否需要对响应进行处理

*/ @Override @SuppressWarnings("all") public boolean supports(MethodParameter methodParameter, Class> aClass) { if(methodParameter.getDeclaringClass().isAnnotationPresent(IgnoreResponseAdvice.class)) { return false; } if(methodParameter.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class)) { return false; } return true; } @Override @SuppressWarnings("all") public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { //定义最终的返回对象 CommonResponse response = new CommonResponse<>(0, ""); if (null == o) { return response; } else if (o instanceof CommonResponse) { response = (CommonResponse) o; } else { response.setData(o); } return response; } }

全局异常捕获处理

/**
 * 

全局异常捕获处理

*/ @Slf4j @RestControllerAdvice public class GlobalExceptionAdvice { @ExceptionHandler(value = Exception.class) public CommonResponse handlerCommerceException( HttpServletRequest req, Exception ex) { CommonResponse response = new CommonResponse<>(-1, "business error"); response.setData(ex.getMessage()); log.error("commerce service has error: [{}]", ex.getMessage(), ex); return response; } }

你可能感兴趣的:(spring,cloud,spring,cloud,eureka,java)