commit 0852760852f994ca263e6957cc08529b430f6284 Author: Haixing Hu Date: Thu Feb 20 15:34:19 2025 +0800 init: 初始化项目 diff --git a/backend/pom.xml b/backend/pom.xml new file mode 100644 index 0000000..f7deefc --- /dev/null +++ b/backend/pom.xml @@ -0,0 +1,161 @@ + + + 4.0.0 + + ltd.qubit + llm-survey-api + 1.0-SNAPSHOT + war + + + 17 + ${java.version} + ${java.version} + UTF-8 + 5.3.31 + 3.5.15 + 2.1.2 + 8.3.0 + 5.1.0 + 2.16.1 + 1.18.30 + 2.0.11 + 1.4.14 + 4.0.1 + + + + + + org.springframework + spring-webmvc + ${spring.version} + + + org.springframework + spring-jdbc + ${spring.version} + + + org.springframework + spring-context-support + ${spring.version} + + + org.springframework + spring-tx + ${spring.version} + + + + + org.mybatis + mybatis + ${mybatis.version} + + + org.mybatis + mybatis-spring + ${mybatis-spring.version} + + + + + com.mysql + mysql-connector-j + ${mysql-connector.version} + + + + + com.zaxxer + HikariCP + ${hikaricp.version} + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + + org.projectlombok + lombok + ${lombok.version} + provided + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + javax.servlet + javax.servlet-api + ${servlet-api.version} + provided + + + + + llm-survey-api + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + ${java.version} + ${project.build.sourceEncoding} + + + org.projectlombok + lombok + ${lombok.version} + + + + + + + + org.apache.maven.plugins + maven-war-plugin + 3.4.0 + + false + + + + + + + + src/main/resources + true + + + src/main/java + + **/*.xml + + true + + + + \ No newline at end of file diff --git a/backend/src/main/java/ltd/qubit/survey/controller/GlobalExceptionHandler.java b/backend/src/main/java/ltd/qubit/survey/controller/GlobalExceptionHandler.java new file mode 100644 index 0000000..ef094e3 --- /dev/null +++ b/backend/src/main/java/ltd/qubit/survey/controller/GlobalExceptionHandler.java @@ -0,0 +1,78 @@ +package ltd.qubit.survey.controller; + +import lombok.extern.slf4j.Slf4j; +import ltd.qubit.survey.model.ErrorInfo; +import org.springframework.dao.DataAccessException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.BindException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * 全局异常处理器 + */ +@Slf4j +@RestControllerAdvice +public class GlobalExceptionHandler { + + /** + * 处理参数校验异常 + */ + @ExceptionHandler(BindException.class) + public ResponseEntity handleBindException(BindException e) { + String message = e.getBindingResult().getFieldErrors().stream() + .map(error -> error.getField() + ": " + error.getDefaultMessage()) + .reduce((a, b) -> a + "; " + b) + .orElse("参数错误"); + + ErrorInfo error = ErrorInfo.of( + "INVALID_ARGUMENT", + "参数校验失败", + message, + e.getObjectName()); + + return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST); + } + + /** + * 处理业务异常 + */ + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleIllegalArgumentException(IllegalArgumentException e) { + log.warn("业务异常", e); + ErrorInfo error = ErrorInfo.of( + "BAD_REQUEST", + e.getMessage(), + e.getClass().getName()); + return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST); + } + + /** + * 处理数据访问异常 + */ + @ExceptionHandler(DataAccessException.class) + public ResponseEntity handleDataAccessException(DataAccessException e) { + log.error("数据访问异常", e); + ErrorInfo error = ErrorInfo.of( + "DATABASE_ERROR", + "数据库访问错误", + e.getMessage(), + e.getClass().getName()); + return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR); + } + + /** + * 处理其他未知异常 + */ + @ExceptionHandler(Exception.class) + public ResponseEntity handleUnknownException(Exception e) { + log.error("系统异常", e); + ErrorInfo error = ErrorInfo.of( + "SYSTEM_ERROR", + "系统错误", + e.getMessage(), + e.getClass().getName()); + return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR); + } +} \ No newline at end of file diff --git a/backend/src/main/java/ltd/qubit/survey/controller/QuestionController.java b/backend/src/main/java/ltd/qubit/survey/controller/QuestionController.java new file mode 100644 index 0000000..e983c75 --- /dev/null +++ b/backend/src/main/java/ltd/qubit/survey/controller/QuestionController.java @@ -0,0 +1,61 @@ +package ltd.qubit.survey.controller; + +import java.util.List; +import lombok.RequiredArgsConstructor; +import ltd.qubit.survey.model.Question; +import ltd.qubit.survey.model.Option; +import ltd.qubit.survey.service.QuestionService; +import ltd.qubit.survey.service.OptionService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 问题控制器 + */ +@RestController +@RequiredArgsConstructor +public class QuestionController { + private final QuestionService questionService; + private final OptionService optionService; + + /** + * 获取用户的问题列表 + * + * @param userId 用户ID + * @return 问题列表 + */ + @GetMapping("/question/user/{userId}") + public List getUserQuestions(@PathVariable Long userId) { + return questionService.getUserQuestions(userId); + } + + /** + * 获取问题的选项列表 + * + * @param questionId 问题ID + * @return 选项列表 + */ + @GetMapping("/question/{questionId}/option") + public List