package com.github.binarywang.demo.wx.mp.controller;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseDto getException() {
return ResponseUtils.error("出错了");
}
}
package com.github.binarywang.demo.wx.mp.controller;
import lombok.Data;
@Data
public class ResponseDto<T> {
private String code;
private String msg;
private T data;
public ResponseDto() {
}
public ResponseDto(String code, String msg) {
this.code = code;
this.msg = msg;
this.data = null;
}
}
package com.github.binarywang.demo.wx.mp.controller;
public class ResponseUtils {
public static ResponseDto error(Object object) {
ResponseDto<Object> objectResponseDto = new ResponseDto<>();
objectResponseDto.setCode("200");
objectResponseDto.setMsg("请求失败");
objectResponseDto.setData(object);
return objectResponseDto;
}
}
package com.github.binarywang.demo.wx.mp.controller;
import com.github.binarywang.demo.wx.mp.entity.ContactUser;
import com.github.binarywang.demo.wx.mp.service.IUserInfoService;
import com.github.binarywang.demo.wx.mp.utils.CommonUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@AllArgsConstructor
@RestController
@RequestMapping(value ="/wx/test/")
public class TestController {
private final IUserInfoService userInfoService;
@GetMapping("/userInfo")
public void userInfo() {
ContactUser userInfo = new ContactUser();
userInfo.setCreateTime(new Date(Long.parseLong(String.valueOf(System.currentTimeMillis()))));
userInfo.setGender(CommonUtils.getGender(null));
throw new RuntimeException();
}
}