mirror of https://github.com/halo-dev/halo
Refactor ApiArchivesController
parent
a08fac24a6
commit
0059dc7a9a
|
@ -14,7 +14,7 @@ import lombok.Data;
|
||||||
public class JsonResult {
|
public class JsonResult {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回的状态码,0:失败,1:成功
|
* 返回的状态码 (Same as HttpStatus.value()).
|
||||||
*/
|
*/
|
||||||
private Integer code;
|
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.model.enums.ResponseStatusEnum;
|
||||||
import cc.ryanc.halo.service.PostService;
|
import cc.ryanc.halo.service.PostService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
@ -74,7 +74,7 @@ public class ApiArchivesController {
|
||||||
@GetMapping(value = "/year")
|
@GetMapping(value = "/year")
|
||||||
public JsonResult archivesYear() {
|
public JsonResult archivesYear() {
|
||||||
final List<Archive> archives = postService.findPostGroupByYear();
|
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);
|
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), archives);
|
||||||
} else {
|
} else {
|
||||||
return new JsonResult(ResponseStatusEnum.EMPTY.getCode(), ResponseStatusEnum.EMPTY.getMsg());
|
return new JsonResult(ResponseStatusEnum.EMPTY.getCode(), ResponseStatusEnum.EMPTY.getMsg());
|
||||||
|
@ -126,13 +126,8 @@ public class ApiArchivesController {
|
||||||
* @return JsonResult
|
* @return JsonResult
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/year/month")
|
@GetMapping(value = "/year/month")
|
||||||
public JsonResult archivesYearAndMonth() {
|
public List<Archive> archivesYearAndMonth() {
|
||||||
final List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
return 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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -143,13 +138,8 @@ public class ApiArchivesController {
|
||||||
* @Param
|
* @Param
|
||||||
**/
|
**/
|
||||||
@GetMapping(value = "/all")
|
@GetMapping(value = "/all")
|
||||||
public JsonResult archivesAllPost() {
|
public List<Archive> archivesAllPost() {
|
||||||
final List<Archive> archive = postService.findAllPost();
|
return 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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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.
|
* Exception handler of controller.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
*/
|
*/
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
public class ControllerExceptionHandler {
|
public class ControllerExceptionHandler {
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
package cc.ryanc.halo.web.interceptor;
|
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.BlogPropertiesEnum;
|
||||||
import cc.ryanc.halo.model.enums.TrueFalseEnum;
|
import cc.ryanc.halo.model.enums.TrueFalseEnum;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.util.HashMap;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS;
|
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()))) {
|
if (StrUtil.equals(request.getHeader(TOKEN), OPTIONS.get(BlogPropertiesEnum.API_TOKEN.getProp()))) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
response.setCharacterEncoding("UTF-8");
|
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
||||||
response.setContentType("application/json;charset=utf-8");
|
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||||
Map<String, Object> map = new HashMap<>(2);
|
JsonResult result = new JsonResult(HttpStatus.BAD_REQUEST.value(), "Invalid Token");
|
||||||
map.put("code", HttpStatus.BAD_REQUEST.value());
|
response.getWriter().write(objectMapper.writeValueAsString(result));
|
||||||
map.put("msg", "Invalid Token");
|
|
||||||
response.getWriter().write(objectMapper.writeValueAsString(map));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue