首页 > 其他分享 >SpringBoot学习-(二十四)SpringBoot配置错误页面(404、500等)

SpringBoot学习-(二十四)SpringBoot配置错误页面(404、500等)

时间:2022-10-11 17:10:01浏览次数:57  
标签:return SpringBoot ApiResponse 404 msg data public 500 String


基本步骤:

  • 添加错误页面配置
  • 书写对应action处理映射

添加错误页面配置

package com.ahut.config;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
*
* @ClassName: ErrorPageConfig
* @Description: 配置错误页面
* @author cheng
* @date
@Configuration
public class ErrorPageConfig

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/400.html");
ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/errorPage/404");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

container.addErrorPages(error400Page, error401Page, error404Page, error500Page);
}
};
}

}

书写对应action处理映射

package com.ahut.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ahut.common.ApiResponse;

/**
*
* @ClassName: ErrorPageAction
* @Description: 错误页面控制器
* @author cheng
* @date
@Controller
@RequestMapping(value = "/errorPage")
public class ErrorPageAction

/**
* 日志管理
*/
private Logger log = LoggerFactory.getLogger(ErrorPageAction.class);

/**
*
* @Title: notFound
* @Description: 配置404
* @return
@RequestMapping(value = "/404")
@ResponseBody
public ApiResponse notFound() {
log.warn("没有找到对应的信息");
return ApiResponse.createFailResponse("没有找到对应的信息");
}

}

其中ApiResponse为自定义实体

package com.ahut.common;

import java.io.Serializable;

/**
*
* @ClassName: ApiResponse
* @Description: api返回结果响应实体类
* @author cheng
* @date
public class ApiResponse implements Serializable

/**
* 序列化
*/
private static final long serialVersionUID = -5178560042946902578L;
/**
* 成功类型
*/
private static final String SUCCESS_TYPE = "SUCCESS";
/**
* 失败类型
*/
private static final String FAIL_TYPE = "FAIL";
/**
* 没有数据提示
*/
private static final String NO_DATA = "NO DATA";
/**
* 成功信息
*/
private static final String SUCCESS_MSG = "REQUEST SUCCESS";
/**
* 失败信息
*/
private static final String FAIL_MSG = "REQUEST FAIL";
/**
* 类型
*/
private String type;
/**
* 信息
*/
private String msg;
/**
* 数据
*/
private Object data;

/**
* 私有化构造方法
*/
private ApiResponse() {
}

/**
* 重写toString
*/
@Override
public String toString() {
return "ApiResponse [type=" + type + ", msg=" + msg + ", data=" + data + "]";
}

/**
*
* @Title: createSuccessResponse
* @Description: 成功返回,包含信息和数据
* @param msg
* @param data
* @return
public static ApiResponse createSuccessResponse(String msg, Object data) {
ApiResponse response = new ApiResponse();
response.setType(SUCCESS_TYPE);
response.setMsg(msg);
response.setData(data);
return response;
}

/**
*
* @Title: createSuccessResponse
* @Description: 成功返回,只包含信息
* @param msg
* @return
public static ApiResponse createSuccessResponse(String msg) {
return createSuccessResponse(msg, NO_DATA);
}

/**
*
* @Title: createSuccessResponse
* @Description: 成功返回,只包含数据
* @param data
* @param clazz
* @return
public static ApiResponse createSuccessResponse(Object data, Class<?> clazz) {
return createSuccessResponse(SUCCESS_MSG, data);
}

/**
*
* @Title: createFailResponse
* @Description: 失败返回,包含信息和数据
* @param msg
* @param data
* @return
public static ApiResponse createFailResponse(String msg, Object data) {
ApiResponse response = new ApiResponse();
response.setType(FAIL_TYPE);
response.setMsg(msg);
response.setData(data);
return response;
}

/**
*
* @Title: createFailResponse
* @Description: 失败返回,只包含信息
* @param msg
* @return
public static ApiResponse createFailResponse(String msg) {
return createFailResponse(msg, NO_DATA);
}

/**
*
* @Title: createFailResponse
* @Description: 失败返回,只包含数据
* @param data
* @param clazz
* @return
public static ApiResponse createFailResponse(Object data, Class<?> clazz) {
return createFailResponse(FAIL_MSG, data);
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

}


标签:return,SpringBoot,ApiResponse,404,msg,data,public,500,String
From: https://blog.51cto.com/u_15824687/5747133

相关文章