新增全局报错处理统 优化代码结构 article基础代码完成
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import com.qf.myafterprojecy.pojo.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
|
||||
import com.qf.myafterprojecy.service.IArticleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@@ -17,14 +18,24 @@ public class ArticleController {
|
||||
@Autowired
|
||||
IArticleService ArticleService;
|
||||
|
||||
@PostMapping
|
||||
public ResponseMessage<Iterable<Article>> getArticle(){
|
||||
return ArticleService.getArticleAll();
|
||||
};
|
||||
@GetMapping
|
||||
public ResponseMessage<Article> geybyID(@RequestBody ArticleDto articleDto){
|
||||
System.out.println("id:"+ articleDto.getArticleid());
|
||||
return ArticleService.getArticleByid(Integer.valueOf(articleDto.getArticleid()));
|
||||
public ResponseMessage<Iterable<Article>> getArticleAllByID(@Validated @RequestBody ArticleDto articleDto){
|
||||
return ArticleService.getArticleAllByID(articleDto);
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,8 +12,42 @@ public class ResponseMessage<T> {
|
||||
this.message = message;
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.qf.myafterprojecy.pojo.dto;
|
||||
|
||||
import javax.persistence.Column;
|
||||
|
||||
public class ArticleDto {
|
||||
private Integer articleid;
|
||||
|
||||
@@ -2,23 +2,41 @@ package com.qf.myafterprojecy.service;
|
||||
|
||||
import com.qf.myafterprojecy.pojo.Article;
|
||||
import com.qf.myafterprojecy.pojo.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
|
||||
import com.qf.myafterprojecy.repository.ArticleRepository;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Service
|
||||
public class ArticleService implements IArticleService{
|
||||
@Autowired
|
||||
ArticleRepository articleRepository;
|
||||
|
||||
@Override
|
||||
public ResponseMessage<Article> getArticleByid(Integer articleid){
|
||||
Article articlenew = articleRepository.findById(articleid).orElseThrow(RuntimeException::new);
|
||||
return ResponseMessage.success(articlenew) ;
|
||||
public ResponseMessage<Iterable<Article>> getArticleAllByID(ArticleDto articleDto){
|
||||
Iterable<Article> Articles = null;
|
||||
if (articleDto.getArticleid()!=null && articleDto.getArticleid()!=0){
|
||||
|
||||
Articles = Collections.singleton(articleRepository.findById(articleDto.getArticleid()).orElseThrow(()-> new RuntimeException("没有该文章")));
|
||||
}else {
|
||||
Articles = articleRepository.findAll();
|
||||
}
|
||||
return ResponseMessage.success(Articles) ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseMessage<Iterable<Article>> getArticleAll(){
|
||||
Iterable<Article> Articles = articleRepository.findAll();
|
||||
return ResponseMessage.success(Articles);
|
||||
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)) ;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,32 @@ package com.qf.myafterprojecy.service;
|
||||
|
||||
import com.qf.myafterprojecy.pojo.Article;
|
||||
import com.qf.myafterprojecy.pojo.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.dto.ArticleDto;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user