[Bugfix] No thread-bound request found. (#743)

This commit is contained in:
Richard
2022-11-04 23:31:41 +08:00
committed by EricZeng
parent ed0efd6bd2
commit b51ffb81a3

View File

@@ -7,6 +7,9 @@ import com.didiglobal.logi.security.service.OplogService;
import com.xiaojukeji.know.streaming.km.core.service.oprecord.OpLogWrapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Service
public class OpLogWrapServiceImpl implements OpLogWrapService {
@@ -18,6 +21,12 @@ public class OpLogWrapServiceImpl implements OpLogWrapService {
@Override
public Integer saveOplogAndIgnoreException(OplogDTO oplogDTO) {
try {
// fix request that cannot find thread binding (issue#743)
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null == servletRequestAttributes) {
servletRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
RequestContextHolder.setRequestAttributes(servletRequestAttributes, true);
}
return oplogService.saveOplog(oplogDTO);
} catch (Exception e) {
log.error("method=saveOplogAndIgnoreException||oplogDTO={}||errMsg=exception.", oplogDTO, e);