feat: init project

This commit is contained in:
sudoooooo 2024-06-04 22:09:07 +08:00
parent 4dc6145068
commit b5622f1199
43 changed files with 1042 additions and 0 deletions

0
build.sh Normal file
View File

121
pom.xml Normal file
View File

@ -0,0 +1,121 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/>
</parent>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-engine</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<modules>
<module>survey-server</module>
<module>survey-common</module>
<module>survey-dal</module>
<module>survey-core</module>
<module>survey-extensions</module>
</modules>
<properties>
<revision>1.0.0</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.compiler.source.version>1.8</java.compiler.source.version>
<java.compiler.target.version>1.8</java.compiler.target.version>
<lombok.version>1.18.8</lombok.version>
<junit.version>4.12</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-common</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-dal</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-core</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-extensions</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>
<build>
<pluginManagement>
<plugins>
<!-- 定义JDK版本 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.compiler.source.version}</source>
<target>${java.compiler.target.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

30
survey-common/pom.xml Normal file
View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-engine</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>survey-common</artifactId>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,25 @@
package com.xiaojusurvey.engine.common.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import java.io.Serializable;
import java.util.List;
@Data
public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = -1166246812384704515L;
@Id
private String id;
private Long createDate;
private Long updateDate;
private Status curStatus;
private List<Status> statusList;
}

View File

@ -0,0 +1,15 @@
package com.xiaojusurvey.engine.common.entity;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class Status implements Serializable {
private static final long serialVersionUID = 2408931688513907276L;
private Long date;
private String status;
}

View File

@ -0,0 +1,15 @@
package com.xiaojusurvey.engine.common.entity;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
@Document("user")
@Data
public class User extends BaseEntity{
private String password;
private String username;
}

View File

@ -0,0 +1,24 @@
package com.xiaojusurvey.engine.common.exception;
import lombok.Data;
@Data
public abstract class BaseException extends RuntimeException {
private static final long serialVersionUID = -1164840626493438227L;
private String errorMsg;
private Integer code;
public BaseException(String errorMsg, Integer code) {
super(errorMsg);
this.code=code;
this.errorMsg=errorMsg;
}
public BaseException(String errorMsg, Integer code,Throwable t) {
super(errorMsg,t);
this.code=code;
this.errorMsg=errorMsg;
}
}

View File

@ -0,0 +1,12 @@
package com.xiaojusurvey.engine.common.exception;
public class DaoException extends BaseException{
public DaoException(String errorMsg, Integer code) {
super(errorMsg, code);
}
public DaoException(String errorMsg, Integer code, Throwable t) {
super(errorMsg, code, t);
}
}

View File

@ -0,0 +1,12 @@
package com.xiaojusurvey.engine.common.exception;
public class ServiceException extends BaseException {
public ServiceException(String errorMsg, Integer code) {
super(errorMsg, code);
}
public ServiceException(String errorMsg, Integer code, Throwable t) {
super(errorMsg, code, t);
}
}

View File

@ -0,0 +1,19 @@
package com.xiaojusurvey.engine.common.rpc;
import lombok.Data;
import java.io.Serializable;
@Data
public class RpcResult<T> implements Serializable {
private static final long serialVersionUID = -3324016200311008221L;
private Integer code = -1;
private String msg;
private T data;
private Boolean success;
}

View File

@ -0,0 +1,48 @@
package com.xiaojusurvey.engine.common.util;
import com.xiaojusurvey.engine.common.rpc.RpcResult;
public class RpcResultUtil {
private static final int SUCCESS = 200;
static final int SYSTEM_ERROR = -1;
private static final String DEFAULT_SUCC_MSG = "success";
public static <T> RpcResult<T> createSuccessResult(T data) {
return createSuccessResult(data, DEFAULT_SUCC_MSG);
}
public static <T> RpcResult<T> createSuccessResult(T data, String message) {
RpcResult<T> result = new RpcResult<>();
result.setCode(SUCCESS);
result.setMsg(message);
result.setData(data);
result.setSuccess(Boolean.TRUE);
return result;
}
public static <T> RpcResult<T> createFailedResult(T failed, Integer errorCode, String msg) {
RpcResult<T> result = new RpcResult<>();
result.setCode(errorCode);
result.setMsg(msg);
result.setData(failed);
result.setSuccess(Boolean.FALSE);
return result;
}
public static <T> RpcResult<T> createFailedResult(int errorCode, String msg) {
RpcResult<T> result = new RpcResult<>();
result.setCode(errorCode);
result.setMsg(msg);
result.setSuccess(Boolean.FALSE);
return result;
}
public static <T> RpcResult<T> createFailedResult(String msg) {
RpcResult<T> result = new RpcResult<>();
result.setCode(SYSTEM_ERROR);
result.setMsg(msg);
result.setSuccess(Boolean.FALSE);
return result;
}
}

35
survey-core/pom.xml Normal file
View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-engine</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>survey-core</artifactId>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-dal</artifactId>
</dependency>
<dependency>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-extensions</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,10 @@
package com.xiaojusurvey.engine.core.user;
import com.xiaojusurvey.engine.common.entity.User;
import java.util.List;
public interface UserService {
List<User> findAllUser();
}

View File

@ -0,0 +1,21 @@
package com.xiaojusurvey.engine.core.user.impl;
import com.xiaojusurvey.engine.common.entity.User;
import com.xiaojusurvey.engine.core.user.UserService;
import com.xiaojusurvey.engine.repository.MongoRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private MongoRepository mongoRepository;
@Override
public List<User> findAllUser() {
return mongoRepository.findAll(User.class);
}
}

31
survey-dal/pom.xml Normal file
View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-engine</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>survey-dal</artifactId>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-common</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,14 @@
package com.xiaojusurvey.engine.repository;
import com.xiaojusurvey.engine.common.entity.BaseEntity;
import java.util.List;
public interface MongoRepository {
<T extends BaseEntity> T save(T saveObject);
<T extends BaseEntity> T findById(Object id, Class<T> entityClass);
<T extends BaseEntity> List<T> findAll(Class<T> entityClass);
}

View File

@ -0,0 +1,32 @@
package com.xiaojusurvey.engine.repository.enums;
public enum ErrorEnum {
PARAM_NULL_ERROR("%s is null",1);
private String errorMsg;
private Integer code;
ErrorEnum(String errorMsg, Integer code) {
this.errorMsg = errorMsg;
this.code = code;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}

View File

@ -0,0 +1,39 @@
package com.xiaojusurvey.engine.repository.impl;
import com.xiaojusurvey.engine.common.entity.BaseEntity;
import com.xiaojusurvey.engine.common.exception.DaoException;
import com.xiaojusurvey.engine.repository.MongoRepository;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import static com.xiaojusurvey.engine.repository.enums.ErrorEnum.PARAM_NULL_ERROR;
@Repository
public class MongoRepositoryImpl implements MongoRepository {
@Resource
private MongoTemplate mongoTemplate;
@Override
public <T extends BaseEntity> T save(T saveObject) {
return null;
}
@Override
public <T extends BaseEntity> T findById(Object id, Class<T> entityClass) {
if (Objects.isNull(id)) {
throw new DaoException(String.format(PARAM_NULL_ERROR.getErrorMsg(), "id"), PARAM_NULL_ERROR.getCode());
}
return mongoTemplate.findById(id, entityClass);
}
@Override
public <T extends BaseEntity> List<T> findAll(Class<T> entityClass) {
return mongoTemplate.findAll(entityClass);
}
}

28
survey-extensions/pom.xml Normal file
View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-engine</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>survey-extensions</artifactId>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,7 @@
package com.xiaojusurvey.engine.extensions.message;
import com.xiaojusurvey.engine.extensions.processor.ExtensionProcessor;
public interface MessageProcessor extends ExtensionProcessor {
}

View File

@ -0,0 +1,22 @@
package com.xiaojusurvey.engine.extensions.message.impl;
import com.xiaojusurvey.engine.extensions.message.MessageProcessor;
import com.xiaojusurvey.engine.extensions.processor.Invocation;
import com.xiaojusurvey.engine.extensions.processor.Result;
import org.springframework.stereotype.Component;
@Component
public class TestMessageProcessor implements MessageProcessor {
@Override
public void before(Invocation invocation) {
}
@Override
public Result after(Result result) {
return new Result();
}
}

View File

@ -0,0 +1,8 @@
package com.xiaojusurvey.engine.extensions.processor;
public interface ExtensionProcessor {
void before(Invocation invocation);
Result after(Result result);
}

View File

@ -0,0 +1,17 @@
package com.xiaojusurvey.engine.extensions.processor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Invocation {
private String methodName;
private Class<?>[] getParameterTypes;
private Object[] getArguments;
}

View File

@ -0,0 +1,14 @@
package com.xiaojusurvey.engine.extensions.processor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
private Object value;
}

View File

@ -0,0 +1,10 @@
package com.xiaojusurvey.engine.extensions.processor.annotation;
import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Message {
}

View File

@ -0,0 +1,9 @@
package com.xiaojusurvey.engine.extensions.processor.annotation;
import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Security {
}

View File

@ -0,0 +1,60 @@
package com.xiaojusurvey.engine.extensions.processor.aop;
import com.xiaojusurvey.engine.extensions.processor.Invocation;
import com.xiaojusurvey.engine.extensions.processor.Result;
import com.xiaojusurvey.engine.extensions.processor.annotation.Message;
import com.xiaojusurvey.engine.extensions.processor.annotation.Security;
import com.xiaojusurvey.engine.extensions.router.MessageProcessorRouter;
import com.xiaojusurvey.engine.extensions.router.SecurityProcessorRouter;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Objects;
@Aspect
@Component
public class ExtensionAspect {
@Autowired
private MessageProcessorRouter messageProcessorRouter;
@Autowired
private SecurityProcessorRouter securityProcessorRouter;
@Pointcut("@annotation(com.xiaojusurvey.engine.extensions.processor.annotation.Message) || @annotation(com.xiaojusurvey.engine.extensions.processor.annotation.Security)")
public void pointcut() {
}
@Around("pointcut()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
Message message = method.getAnnotation(Message.class);
Security security = method.getAnnotation(Security.class);
if (!Objects.isNull(message)) {
Invocation invocation = new Invocation(method.getName(), method.getParameterTypes(), pjp.getArgs());
messageProcessorRouter.before(invocation);
}
if (!Objects.isNull(security)) {
Invocation invocation = new Invocation(method.getName(), method.getParameterTypes(), pjp.getArgs());
securityProcessorRouter.before(invocation);
}
Result result = new Result(pjp.proceed(pjp.getArgs()));
if (!Objects.isNull(message)) {
result = messageProcessorRouter.after(result);
}
if (!Objects.isNull(security)) {
result = securityProcessorRouter.after(result);
}
return result.getValue();
}
}

View File

@ -0,0 +1,21 @@
package com.xiaojusurvey.engine.extensions.router;
import com.xiaojusurvey.engine.extensions.processor.Invocation;
import com.xiaojusurvey.engine.extensions.processor.Result;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public abstract class BaseRouter implements ApplicationContextAware {
protected ApplicationContext applicationContext;
public abstract void before(Invocation invocation);
public abstract Result after(Result result);
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

View File

@ -0,0 +1,42 @@
package com.xiaojusurvey.engine.extensions.router;
import com.xiaojusurvey.engine.extensions.message.MessageProcessor;
import com.xiaojusurvey.engine.extensions.processor.Invocation;
import com.xiaojusurvey.engine.extensions.processor.Result;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class MessageProcessorRouter extends BaseRouter implements InitializingBean {
private List<MessageProcessor> messageProcessorList = new ArrayList<>();
@Override
public void afterPropertiesSet() throws Exception {
Map<String, MessageProcessor> messageProcessorMap = applicationContext.getBeansOfType(MessageProcessor.class);
if (!CollectionUtils.isEmpty(messageProcessorMap)) {
messageProcessorMap.entrySet().stream().forEach(e -> messageProcessorList.add(e.getValue()));
}
}
@Override
public void before(Invocation invocation) {
for (MessageProcessor messageProcessor : messageProcessorList) {
messageProcessor.before(invocation);
}
}
@Override
public Result after(Result result) {
for (MessageProcessor messageProcessor : messageProcessorList) {
result = messageProcessor.after(result);
}
return result;
}
}

View File

@ -0,0 +1,41 @@
package com.xiaojusurvey.engine.extensions.router;
import com.xiaojusurvey.engine.extensions.processor.Invocation;
import com.xiaojusurvey.engine.extensions.processor.Result;
import com.xiaojusurvey.engine.extensions.security.SecurityProcessor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class SecurityProcessorRouter extends BaseRouter implements InitializingBean {
private List<SecurityProcessor> securityProcessorList = new ArrayList<>();
@Override
public void afterPropertiesSet() throws Exception {
Map<String, SecurityProcessor> messageProcessorMap = applicationContext.getBeansOfType(SecurityProcessor.class);
if (!CollectionUtils.isEmpty(messageProcessorMap)) {
messageProcessorMap.entrySet().stream().forEach(e -> securityProcessorList.add(e.getValue()));
}
}
@Override
public void before(Invocation invocation) {
for (SecurityProcessor securityProcessor : securityProcessorList) {
securityProcessor.before(invocation);
}
}
@Override
public Result after(Result result) {
for (SecurityProcessor securityProcessor : securityProcessorList) {
result= securityProcessor.after(result);
}
return result;
}
}

View File

@ -0,0 +1,7 @@
package com.xiaojusurvey.engine.extensions.security;
import com.xiaojusurvey.engine.extensions.processor.ExtensionProcessor;
public interface SecurityProcessor extends ExtensionProcessor {
}

View File

@ -0,0 +1,21 @@
package com.xiaojusurvey.engine.extensions.security.impl;
import com.xiaojusurvey.engine.extensions.processor.Invocation;
import com.xiaojusurvey.engine.extensions.processor.Result;
import com.xiaojusurvey.engine.extensions.security.SecurityProcessor;
import org.springframework.stereotype.Component;
@Component
public class TestSecurityProcessor implements SecurityProcessor {
@Override
public void before(Invocation invocation) {
}
@Override
public Result after(Result result) {
return new Result();
}
}

71
survey-server/pom.xml Normal file
View File

@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-engine</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>survey-server</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.xiaojusurvey.engine</groupId>
<artifactId>survey-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,12 @@
package com.xiaojusurvey.engine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SurveyApplication {
public static void main(String[] args) {
SpringApplication.run(SurveyApplication.class, args);
}
}

View File

@ -0,0 +1,38 @@
package com.xiaojusurvey.engine.aop;
import com.xiaojusurvey.engine.common.exception.DaoException;
import com.xiaojusurvey.engine.common.exception.ServiceException;
import com.xiaojusurvey.engine.common.util.RpcResultUtil;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(-10000)
@Log4j2
public class ExceptionAspect {
private static final String DEFAULT_ERROR = "未知异常,请与管理员联系";
@Pointcut("execution(public * com.xiaojusurvey.*.controller..*Controller.*(..))")
public void controllerPointcut() {
}
@Around("controllerPointcut()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) {
try {
return proceedingJoinPoint.proceed();
} catch (DaoException | ServiceException e) {
log.error("dao or service error", e);
return RpcResultUtil.createFailedResult(e.getCode(), e.getErrorMsg());
} catch (Throwable t) {
log.error("unkown error:", t);
return RpcResultUtil.createFailedResult(DEFAULT_ERROR);
}
}
}

View File

@ -0,0 +1,23 @@
package com.xiaojusurvey.engine.controller;
import com.xiaojusurvey.engine.common.entity.User;
import com.xiaojusurvey.engine.common.rpc.RpcResult;
import com.xiaojusurvey.engine.common.util.RpcResultUtil;
import com.xiaojusurvey.engine.core.user.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RequestMapping("/api/user")
@RestController
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/findAllUser")
public RpcResult<List<User>> findAllUser() {
return RpcResultUtil.createSuccessResult(userService.findAllUser());
}
}

View File

@ -0,0 +1,15 @@
package com.xiaojusurvey.engine.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}

View File

@ -0,0 +1,17 @@
package com.xiaojusurvey.engine.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SurveyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/api/**")
.excludePathPatterns("/api/auth/login", "/api/auth/register");
}
}

View File

@ -0,0 +1,4 @@
server.port=8080
spring.data.mongodb.uri=
spring.data.mongodb.database=

View File

@ -0,0 +1,4 @@
server.port=8080
spring.data.mongodb.uri=
spring.data.mongodb.database=

View File

@ -0,0 +1 @@
server.port=8080

View File

@ -0,0 +1 @@
spring.profiles.active=@spring.profiles.active@

View File

@ -0,0 +1,46 @@
package com.xiaojusurvey.engine.controller;
import com.alibaba.fastjson.JSON;
import com.xiaojusurvey.engine.SurveyApplication;
import com.xiaojusurvey.engine.common.entity.User;
import com.xiaojusurvey.engine.common.rpc.RpcResult;
import com.xiaojusurvey.engine.core.user.UserService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
public class UserControllerTest {
@InjectMocks
private UserController userController;
@Mock
private UserService userService;
@Test
public void testFindAllUser() {
Mockito.when(userService.findAllUser()).thenReturn(null);
RpcResult<List<User>> result = userController.findAllUser();
Assert.assertTrue(result.getSuccess());
Assert.assertEquals(new Integer(200), result.getCode());
List<User> userList = new ArrayList<>();
User user = new User();
user.setPassword("1111");
user.setUsername("tom");
userList.add(user);
Mockito.when(userService.findAllUser()).thenReturn(userList);
result = userController.findAllUser();
System.out.println( JSON.toJSONString(result));
Assert.assertEquals(1, result.getData().size());
Assert.assertEquals("1111", result.getData().get(0).getPassword());
Assert.assertEquals("tom", result.getData().get(0).getUsername());
}
}