com.yuweix.tripod.boot.exception.ExceptionAutoConfiguration Maven / Gradle / Ivy
package com.yuweix.tripod.boot.exception;
import com.yuweix.tripod.core.Response;
import com.yuweix.tripod.core.exception.ExceptionHandler;
import com.yuweix.tripod.core.exception.ExceptionViewResolver;
import com.yuweix.tripod.core.json.Json;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.AbstractView;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author yuwei
*/
@Configuration
@ConditionalOnProperty(name = "tripod.boot.exception.enabled")
public class ExceptionAutoConfiguration {
@Configuration
@ConditionalOnProperty(name = "tripod.boot.exception.handler.enabled", matchIfMissing = true)
protected static class ErrorControllerConfiguration {
@Value("${tripod.boot.exception.errorCode:}")
private String errorCode;
@Controller
public class ErrorController {
@Resource
private Json json;
@RequestMapping(value = { "/error", "/error/**" })
@ResponseBody
public String toErrorPage(HttpServletResponse response) {
int status = response.getStatus();
HttpStatus httpStatus = HttpStatus.valueOf(status);
Response resp = new Response<>(errorCode == null || "".equals(errorCode) ? "" + status : errorCode
, httpStatus.getReasonPhrase() + "[" + status + "]");
return json.toJSONString(resp);
}
}
@ConditionalOnMissingBean(ExceptionViewResolver.class)
@Bean
public ExceptionViewResolver exceptionViewResolver(Json json) {
return new ExceptionViewResolver() {
@SuppressWarnings("unchecked")
@Override
public ModelAndView createView(String content) {
AbstractView view = new AbstractView() {
@Override
protected void renderMergedOutputModel(Map map, HttpServletRequest req, HttpServletResponse resp) throws Exception {
resp.setContentType("application/json; charset=" + StandardCharsets.UTF_8);
ServletOutputStream out = resp.getOutputStream();
out.write(json.toJSONString(map).getBytes(StandardCharsets.UTF_8));
out.flush();
}
};
String text = json.toJSONString(new Response(errorCode == null || "".equals(errorCode) ? "500" : errorCode, content));
Map attributes = json.parseObject(text, Map.class);
view.setAttributesMap(attributes);
return new ModelAndView(view);
}
};
}
}
@ConditionalOnMissingBean(ClassMessagePair.class)
@ConfigurationProperties(prefix = "tripod.boot.exception", ignoreUnknownFields = true)
@Bean
public ClassMessagePair classMessagePair() {
return new ClassMessagePair() {
private Map map = new HashMap<>();
@Override
public Map getDefaultMessage() {
return map;
}
};
}
@ConditionalOnMissingBean(ExceptionHandler.class)
@Bean
public ExceptionHandler exceptionHandler(ClassMessagePair classMessagePair, ExceptionViewResolver viewResolver
, @Value("${tripod.boot.exception.showExceptionName:false}") boolean showExceptionName) {
Map, String> errorMsgMap = new HashMap<>();
Map classMessageMap = classMessagePair.getDefaultMessage();
if (classMessageMap != null) {
Set> entrySet = classMessageMap.entrySet();
for (Map.Entry entry : entrySet) {
try {
errorMsgMap.put(Class.forName(entry.getKey()), entry.getValue());
} catch (ClassNotFoundException ignored) {
}
}
}
ExceptionHandler exceptionHandler = new ExceptionHandler();
exceptionHandler.setViewResolver(viewResolver);
exceptionHandler.setErrorMsgMap(errorMsgMap);
exceptionHandler.setShowExceptionName(showExceptionName);
return exceptionHandler;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy