解決方式
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 配置JSON消息轉(zhuǎn)換器 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
將Java對象轉(zhuǎn)換為JSON格式的響應(yīng),使用spring-mvc.xml配置適當(dāng)?shù)南⑥D(zhuǎn)換器。文章來源地址http://www.zghlxwxcb.cn/news/detail-696122.html
問題出處的相關(guān)類如下
interceptor如下
package com.ekgc.interceptor;
import com.ekgc.exception.UnLoginException;
import com.ekgc.pojo.SysUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 登錄攔截器
* 1.實(shí)現(xiàn) HandlerInterceptor接口
* 2.實(shí)現(xiàn)接口方法
* 3.在springmvc.xml中配置攔截器
* @author Magic
* @version 1.0
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...");
// 記錄請求處理開始時(shí)間
request.setAttribute("startTime", System.currentTimeMillis());
// 檢查用戶是否已經(jīng)登錄
if (!isLoggedIn(request)) {
//拋出未登錄異常
throw new UnLoginException("您還沒有登錄?。。?);
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...");
// 計(jì)算請求處理時(shí)間
long startTime = (long) request.getAttribute("startTime");
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
System.out.println("Request execution time: " + executionTime + " ms");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion...");
}
private boolean isLoggedIn(HttpServletRequest request) {
// 檢查用戶是否已登錄的邏輯
HttpSession session = request.getSession();
SysUser user = (SysUser) session.getAttribute("user");
// 返回true表示已登錄,false表示未登錄
if (user == null) {
return false;
}
return true;
}
}
?自定義異常類
package com.ekgc.exception;
/**
* @author Magic
* @version 1.0
*/
public class UnLoginException extends RuntimeException{
public UnLoginException(String message) {
super(message);
}
}
?異常處理類
package com.ekgc.exception;
import com.ekgc.response.RespBody;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 登錄異常處理類
* @author Magic
* @version 1.0
*/
@ControllerAdvice
public class LoginExceptionHandler {
@ExceptionHandler(UnLoginException.class)
@ResponseBody
public RespBody<String> loginExceptionHandler(UnLoginException e) {
String message = e.getMessage();
System.out.println(message);
return new RespBody<String>(-1,message,message);
}
}
文章來源:http://www.zghlxwxcb.cn/news/detail-696122.html
到了這里,關(guān)于解決攔截器拋出異常處理類的500狀態(tài)碼Html默認(rèn)格式響應(yīng) !的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!