注解来自于 spring-web-5.3.21.jar
例子1
@RestController
@RequestMapping("/v1/alg")
public class AlgController {
@PostMapping(value = "/task/{taskNo}", produces = "application/json; charset=utf-8")
@ResponseBody
public BaseRep<Boolean> insertTaskResult(
@PathVariable @NotBlank String taskNo,
@RequestBody Object result) {
例子2
@Controller
@RequestMapping("")
public class GatewayController {
@RequestMapping(value = "/proxy", method = { RequestMethod.GET,
RequestMethod.POST }, produces = "application/json; charset=utf-8")
@ResponseBody
@SkipXss
public BaseResponse<Object> proxy(@RequestParam(value = "facade") String facade,
@RequestParam(value = "method") String method,
@RequestParam(value = "paramJson") String paramJson,
@RequestParam(value = "env") String env) {
get发送的时候 ,可以在body里填数据么?
全局监控
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 处理自定义的业务异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = BizException.class)
@ResponseBody
public BaseRep bizExceptionHandler(HttpServletRequest req, BizException e){
LogUtil.error(LOGGER,e,"发生异常!BizException,code="+e.getErrorEnum()+",msg="+e.getMessage());
BaseRep baseRep = new BaseRep();
baseRep.setSuccess(false);
baseRep.setResultCode(e.getErrorEnum().getCode());
baseRep.setResultMessage(e.getErrorEnum().getDesc());
return baseRep;
}
/**
* 处理其他异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = Throwable.class)
@ResponseBody
public BaseRep exceptionHandler(HttpServletRequest req, Throwable e) throws IOException {
LogUtil.error(LOGGER,e,"异常!Throwable,req="+req.getRequestURL()+",input="+req.getInputStream()+",msg="+e.getMessage());