thread local - Spring and ThreadLocal -
i have spring web app. whenever in app, there error, put error in error context (code given below). then, retrieve error in exception handler , return message. now, input a, if error message "person not there" now, first call spring web app, right output: "person not there" however, second call: output : "person not there" , "person not there" thus, remembers last request though using threadlocal. can please help
my errorcontext code:
public class errorcontext {
private static threadlocal<errorcontext> thisobj = new threadlocal<errorcontext>(); /** * list of errors. */ private arraylist<applicationerror> errorlist; /** * default private constructor. */ private errorcontext() { errorlist = new arraylist<applicationerror>(); } /** * initialize threadlocal variable. */ public static void initialize() { if (thisobj.get() == null) thisobj.set(new errorcontext()); else { errorcontext errorcontext = (errorcontext) thisobj.get(); if (errorcontext.geterrors() != null) errorcontext.geterrors().clear(); } } /** * returns new instance of class. * * @return {@link errorcontext}. */ public static errorcontext getinstance() { if (thisobj.get() == null) { thisobj.set(new errorcontext()); } return (errorcontext) thisobj.get(); } /** * add error code list. * * @param errorcode * errorcode obtained. */ public void adderror(final applicationerror error) { errorlist.add(error); } /** * return list of errorcodes. * * @return list of error codes. */ public list<applicationerror> geterrors() { return errorlist; } /** * @author anbapri resets context. */ public static void resetcontext() { thisobj.set(new errorcontext()); errorcontext errorcontext = (errorcontext) thisobj.get(); if (errorcontext.geterrors() != null) errorcontext.geterrors().clear(); } /** * returns true if errorcontext has error, otherwise false. * * @return */ public boolean haserror() { return !errorlist.isempty(); }
}
my response handler {
package com.db.wscis.core.web.handler;
import java.util.arraylist; import java.util.list; import java.util.locale;
import javax.servlet.http.httpservletrequest;
import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.messagesource; import org.springframework.context.i18n.localecontextholder; import org.springframework.http.httpstatus; import org.springframework.web.bind.annotation.controlleradvice; import org.springframework.web.bind.annotation.exceptionhandler; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.bind.annotation.responsestatus;
import com.db.wscis.core.util.exception.applicationerror; import com.db.wscis.core.util.exception.applicationexception; import com.db.wscis.core.util.exception.errorcontext; import com.db.wscis.core.web.model.applicationerrorres; import com.db.wscis.core.web.model.baseresponse; import com.db.wscis.core.web.model.responseobject;
@controlleradvice public class restexceptionprocessor {
private static final log logger = logfactory .getlog(restexceptionprocessor.class); @autowired private messagesource messagesource; @exceptionhandler(exception.class) @responsestatus(value = httpstatus.not_found) @responsebody public responseobject handleexception(httpservletrequest req, exception excpt) { logger.error("",excpt); // locale locale = localecontextholder.getlocale(); responseobject res = new responseobject(); // string errormessagedesc; res.setresultmessage("failure"); //business validation exception if (excpt instanceof applicationexception) { list<applicationerror> errormessages = errorcontext.getinstance() .geterrors(); list<applicationerrorres> errormessageres=new arraylist<applicationerrorres>(); for(int i=0;i<errormessages.size();i++){ applicationerrorres apperrorres= mapapplicationerrortoapplicationerrorres(errormessages.get(i)); errormessageres.add(apperrorres); } res.setapplicationerrors(errormessageres); } //other generic exception else { arraylist<exception> exceptionlist=new arraylist<exception>(); exceptionlist.add(excpt); res.setexceptionlist(exceptionlist); } return res; } private applicationerrorres mapapplicationerrortoapplicationerrorres(applicationerror apperror){ applicationerrorres res=new applicationerrorres(); res.seterrorcode(apperror.geterrorcode()); res.seterrorlevel(apperror.geterrorlevel()); res.seterrormessage(apperror.geterrormessage()); return res; }
} }
the way fix errorcontext.resetcontext() after end of exception handler.
Comments
Post a Comment