新增全局报错处理统 优化代码结构 article基础代码完成

This commit is contained in:
qingfeng1121
2025-10-09 17:22:00 +08:00
parent 6841ba67f1
commit 92c604e1f5
7 changed files with 119 additions and 16 deletions

View File

@@ -54,6 +54,11 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<!--参数校验-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
@@ -65,6 +70,7 @@
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<build> <build>

View File

@@ -0,0 +1,13 @@
package com.qf.myafterprojecy;
import com.qf.myafterprojecy.pojo.ResponseMessage;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value=Exception.class)
public ResponseMessage<String> handleException(Exception e){
return new ResponseMessage<>(500,"服务器异常",e.getMessage());
}
}

View File

@@ -5,6 +5,7 @@ import com.qf.myafterprojecy.pojo.ResponseMessage;
import com.qf.myafterprojecy.pojo.dto.ArticleDto; import com.qf.myafterprojecy.pojo.dto.ArticleDto;
import com.qf.myafterprojecy.service.IArticleService; import com.qf.myafterprojecy.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@@ -17,14 +18,24 @@ public class ArticleController {
@Autowired @Autowired
IArticleService ArticleService; IArticleService ArticleService;
@PostMapping
public ResponseMessage<Iterable<Article>> getArticle(){
return ArticleService.getArticleAll();
};
@GetMapping @GetMapping
public ResponseMessage<Article> geybyID(@RequestBody ArticleDto articleDto){ public ResponseMessage<Iterable<Article>> getArticleAllByID(@Validated @RequestBody ArticleDto articleDto){
System.out.println("id:"+ articleDto.getArticleid()); return ArticleService.getArticleAllByID(articleDto);
return ArticleService.getArticleByid(Integer.valueOf(articleDto.getArticleid())); }
@PutMapping
public ResponseMessage<Article> UpdateArticle(@RequestBody ArticleDto articleDto){
return ArticleService.SaveArticle(articleDto);
};
@PostMapping
public ResponseMessage<Article> AddArticle(@RequestBody ArticleDto articleDto){
return ArticleService.SaveArticle(articleDto);
}
@DeleteMapping
public ResponseMessage<Article> DeleteArticle(@RequestBody ArticleDto articleDto){
return ArticleService.deleteArticle(articleDto.getArticleid());
} }
} }

View File

@@ -12,8 +12,42 @@ public class ResponseMessage<T> {
this.message = message; this.message = message;
this.data = data; this.data = data;
} }
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
// 接口请求成功 // 接口请求成功
public static <T> ResponseMessage<T> success(T data) { public static <T> ResponseMessage<T> success(T data) {
return new ResponseMessage(HttpStatus.OK.value(), "success", data); return new ResponseMessage(HttpStatus.OK.value(), "success", data);
} }
public static <T> ResponseMessage<T> Delete(Boolean delete) {
return new ResponseMessage(HttpStatus.OK.value(), "delete", delete);
}
public static <T> ResponseMessage<T> Save(Boolean save) {
return new ResponseMessage(HttpStatus.OK.value(), "save", save);
}
} }

View File

@@ -1,6 +1,5 @@
package com.qf.myafterprojecy.pojo.dto; package com.qf.myafterprojecy.pojo.dto;
import javax.persistence.Column;
public class ArticleDto { public class ArticleDto {
private Integer articleid; private Integer articleid;

View File

@@ -2,23 +2,41 @@ package com.qf.myafterprojecy.service;
import com.qf.myafterprojecy.pojo.Article; import com.qf.myafterprojecy.pojo.Article;
import com.qf.myafterprojecy.pojo.ResponseMessage; import com.qf.myafterprojecy.pojo.ResponseMessage;
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
import com.qf.myafterprojecy.repository.ArticleRepository; import com.qf.myafterprojecy.repository.ArticleRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collections;
@Service @Service
public class ArticleService implements IArticleService{ public class ArticleService implements IArticleService{
@Autowired @Autowired
ArticleRepository articleRepository; ArticleRepository articleRepository;
@Override @Override
public ResponseMessage<Article> getArticleByid(Integer articleid){ public ResponseMessage<Iterable<Article>> getArticleAllByID(ArticleDto articleDto){
Article articlenew = articleRepository.findById(articleid).orElseThrow(RuntimeException::new); Iterable<Article> Articles = null;
return ResponseMessage.success(articlenew) ; if (articleDto.getArticleid()!=null && articleDto.getArticleid()!=0){
Articles = Collections.singleton(articleRepository.findById(articleDto.getArticleid()).orElseThrow(()-> new RuntimeException("没有该文章")));
}else {
Articles = articleRepository.findAll();
} }
@Override
public ResponseMessage<Iterable<Article>> getArticleAll(){
Iterable<Article> Articles = articleRepository.findAll();
return ResponseMessage.success(Articles) ; return ResponseMessage.success(Articles) ;
} }
@Override
public ResponseMessage<Article> SaveArticle(ArticleDto article) {
Article articlenew = new Article();
BeanUtils.copyProperties(article,articlenew);
return ResponseMessage.Save(articleRepository.save(articlenew)!= null);
}
@Override
public ResponseMessage<Article> deleteArticle(Integer articleid) {
return ResponseMessage.Delete(articleRepository.existsById(articleid)) ;
}
} }

View File

@@ -2,10 +2,32 @@ package com.qf.myafterprojecy.service;
import com.qf.myafterprojecy.pojo.Article; import com.qf.myafterprojecy.pojo.Article;
import com.qf.myafterprojecy.pojo.ResponseMessage; import com.qf.myafterprojecy.pojo.ResponseMessage;
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
public interface IArticleService { public interface IArticleService {
ResponseMessage<Article> getArticleByid(Integer articleid); /**
* 根据id查询 如果id为空查询所有
*
* @param articleDto
* @return ResponseMessage<Article>
*/
ResponseMessage<Iterable<Article>> getArticleAllByID(ArticleDto articleDto);
/**
* 新增(id=0)
* 修改 (id=修改数据的id)
* @param article
* @return ResponseMessage<Article>
*/
ResponseMessage<Article> SaveArticle(ArticleDto article);
/**
* 删除
* @param articleid
* @return ResponseMessage<Article>
*/
ResponseMessage<Article> deleteArticle(Integer articleid);
ResponseMessage<Iterable<Article>> getArticleAll();
} }