mirror of https://github.com/halo-dev/halo
Refactor ApiArchivesController
parent
a08fac24a6
commit
0059dc7a9a
|
@ -14,7 +14,7 @@ import lombok.Data;
|
|||
public class JsonResult {
|
||||
|
||||
/**
|
||||
* 返回的状态码,0:失败,1:成功
|
||||
* 返回的状态码 (Same as HttpStatus.value()).
|
||||
*/
|
||||
private Integer code;
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import cc.ryanc.halo.model.dto.JsonResult;
|
|||
import cc.ryanc.halo.model.enums.ResponseStatusEnum;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
@ -74,7 +74,7 @@ public class ApiArchivesController {
|
|||
@GetMapping(value = "/year")
|
||||
public JsonResult archivesYear() {
|
||||
final List<Archive> archives = postService.findPostGroupByYear();
|
||||
if (null != archives && archives.size() > 0) {
|
||||
if (!CollectionUtils.isEmpty(archives)) {
|
||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), archives);
|
||||
} else {
|
||||
return new JsonResult(ResponseStatusEnum.EMPTY.getCode(), ResponseStatusEnum.EMPTY.getMsg());
|
||||
|
@ -126,13 +126,8 @@ public class ApiArchivesController {
|
|||
* @return JsonResult
|
||||
*/
|
||||
@GetMapping(value = "/year/month")
|
||||
public JsonResult archivesYearAndMonth() {
|
||||
final List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
if (null != archives && archives.size() > 0) {
|
||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), archives);
|
||||
} else {
|
||||
return new JsonResult(ResponseStatusEnum.EMPTY.getCode(), ResponseStatusEnum.EMPTY.getMsg());
|
||||
}
|
||||
public List<Archive> archivesYearAndMonth() {
|
||||
return postService.findPostGroupByYearAndMonth();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -143,13 +138,8 @@ public class ApiArchivesController {
|
|||
* @Param
|
||||
**/
|
||||
@GetMapping(value = "/all")
|
||||
public JsonResult archivesAllPost() {
|
||||
final List<Archive> archive = postService.findAllPost();
|
||||
if (null != archive && archive.size() > 0) {
|
||||
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), archive);
|
||||
} else {
|
||||
return new JsonResult(ResponseStatusEnum.EMPTY.getCode(), ResponseStatusEnum.EMPTY.getMsg());
|
||||
}
|
||||
public List<Archive> archivesAllPost() {
|
||||
return postService.findAllPost();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package cc.ryanc.halo.web.controller.base;
|
||||
|
||||
import cc.ryanc.halo.model.dto.JsonResult;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.AbstractMappingJacksonResponseBodyAdvice;
|
||||
|
||||
/**
|
||||
* Controller adivce for comment result.
|
||||
*
|
||||
* @author johnniang
|
||||
*/
|
||||
@ControllerAdvice("cc.ryanc.halo.web.controller.api")
|
||||
public class CommonResultControllerAdvice extends AbstractMappingJacksonResponseBodyAdvice {
|
||||
|
||||
|
||||
@Override
|
||||
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer,
|
||||
MediaType contentType,
|
||||
MethodParameter returnType,
|
||||
ServerHttpRequest request,
|
||||
ServerHttpResponse response) {
|
||||
// Get return body
|
||||
Object returnBody = bodyContainer.getValue();
|
||||
|
||||
if (returnBody instanceof JsonResult) {
|
||||
// If the return body is instance of JsonResult
|
||||
JsonResult jsonResult = (JsonResult) returnBody;
|
||||
response.setStatusCode(HttpStatus.resolve(jsonResult.getCode()));
|
||||
return;
|
||||
}
|
||||
|
||||
// Normal return body
|
||||
HttpStatus okStatus = HttpStatus.OK;
|
||||
JsonResult jsonResult = new JsonResult(okStatus.value(), okStatus.getReasonPhrase(), returnBody);
|
||||
bodyContainer.setValue(jsonResult);
|
||||
response.setStatusCode(okStatus);
|
||||
}
|
||||
}
|
|
@ -21,6 +21,8 @@ import java.util.Map;
|
|||
|
||||
/**
|
||||
* Exception handler of controller.
|
||||
*
|
||||
* @author johnniang
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class ControllerExceptionHandler {
|
||||
|
|
|
@ -1,18 +1,19 @@
|
|||
package cc.ryanc.halo.web.interceptor;
|
||||
|
||||
import cc.ryanc.halo.model.dto.JsonResult;
|
||||
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
|
||||
import cc.ryanc.halo.model.enums.TrueFalseEnum;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS;
|
||||
|
||||
|
@ -41,12 +42,10 @@ public class ApiInterceptor implements HandlerInterceptor {
|
|||
if (StrUtil.equals(request.getHeader(TOKEN), OPTIONS.get(BlogPropertiesEnum.API_TOKEN.getProp()))) {
|
||||
return true;
|
||||
} else {
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("application/json;charset=utf-8");
|
||||
Map<String, Object> map = new HashMap<>(2);
|
||||
map.put("code", HttpStatus.BAD_REQUEST.value());
|
||||
map.put("msg", "Invalid Token");
|
||||
response.getWriter().write(objectMapper.writeValueAsString(map));
|
||||
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
JsonResult result = new JsonResult(HttpStatus.BAD_REQUEST.value(), "Invalid Token");
|
||||
response.getWriter().write(objectMapper.writeValueAsString(result));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue