提交 f3b95e44 编写于 作者: 武汉红喜's avatar 武汉红喜

web

上级 16369e7a
......@@ -31,4 +31,8 @@ public class ResultHelper {
}
}
public static Result newResult(int code, String message) {
return new Result(code, message);
}
}
spring:
kafka:
bootstrap-servers: 127.0.0.1:9092
producer:
value-serializer:
consumer:
group-id: testGroup
auto-offset-reset: earliest
\ No newline at end of file
auto-offset-reset: earliest
value-deserializer:
\ No newline at end of file
package org.hongxi.whatsmars.boot.sample.web.controller;
import org.hongxi.whatsmars.boot.sample.web.model.JsonResponse;
import org.hongxi.whatsmars.common.result.Result;
import org.hongxi.whatsmars.common.result.ResultHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -14,9 +15,9 @@ import org.springframework.web.bind.annotation.RestController;
public class SampleController {
@GetMapping("/hello")
public JsonResponse<String> hello(@RequestParam String userId,
@RequestParam Integer age) {
public Result<String> hello(@RequestParam String userId,
@RequestParam Integer age) {
String hello = userId + "," + age;
return JsonResponse.success(hello);
return ResultHelper.newSuccessResult(hello);
}
}
package org.hongxi.whatsmars.boot.sample.web.exception;
import lombok.extern.slf4j.Slf4j;
import org.hongxi.whatsmars.boot.sample.web.model.JsonResponse;
import org.hongxi.whatsmars.common.result.Result;
import org.hongxi.whatsmars.common.result.ResultHelper;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
......@@ -21,19 +22,19 @@ public class DefaultExceptionHandler {
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(BusinessException.class)
@ResponseBody
public JsonResponse handleLogicException(HttpServletRequest request, BusinessException e) {
public Result handleLogicException(HttpServletRequest request, BusinessException e) {
log.error("business exception handled, request:{}", request.getRequestURI(), e);
return JsonResponse.error(e.getCode(), e.getMsg());
return ResultHelper.newResult(e.getCode(), e.getMsg());
}
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(Exception.class)
@ResponseBody
public JsonResponse handleException(HttpServletRequest request, Exception e) throws Exception {
public Result handleException(HttpServletRequest request, Exception e) throws Exception {
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) {
throw e;
}
log.error("exception handled, request:{}", request.getRequestURI(), e);
return JsonResponse.error(500, e.getMessage());
return ResultHelper.newResult(500, e.getMessage());
}
}
package org.hongxi.whatsmars.boot.sample.web.model;
/**
* Created by shenhongxi on 2020/8/16.
*/
public class JsonResponse<T> {
public static final int SUCCESS_CODE = 0;
// 错误码,0表示成功
private int code;
// 错误原因
private String msg;
// 服务器下发数据时间
private long millisecond;
private T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public long getMillisecond() {
return millisecond;
}
public void setMillisecond(long millisecond) {
this.millisecond = millisecond;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static <T> JsonResponse success(T value) {
JsonResponse<T> response = new JsonResponse<>();
response.setCode(SUCCESS_CODE);
response.setMsg("ok");
response.setMillisecond(System.currentTimeMillis());
response.setData(value);
return response;
}
public static JsonResponse error(int ec, String em) {
JsonResponse response = new JsonResponse<>();
response.setCode(ec);
response.setMsg(em);
response.setMillisecond(System.currentTimeMillis());
return response;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册