Spring Cloud zuul自定義異常信息


只需要加上一個controller,代碼如下:

 

package com.wdlx.filter;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/error")
public class GlobalErrorController extends AbstractErrorController {
	@Autowired
	public GlobalErrorController(ErrorAttributes errorAttributes) {
		super(errorAttributes);
	}

	@Override
	public String getErrorPath() {
		return "/error";
	}

	@RequestMapping(produces = MediaType.ALL_VALUE)
	public ResponseEntity error(HttpServletRequest request) {
		HttpStatus status = getStatus(request);
		if (status.is5xxServerError()) {
			// log maybe
		}
		return new ResponseEntity(status);
	}
}

  不過由於Spring Boot版本不同,比如我在1.3.5測試通過在1.4就不行,於是我去掉了

produces = MediaType.ALL_VALUE這個配置,就可以了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM