我已使用以下代码实现了“每个请求过滤器一次”:
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
{
httpRequestWrapper = Optional.ofNullable(new HttpRequestWrapper(request));
if(httpRequestWrapper.isPresent())
{
Message message = objectMapper.readValue(httpRequestWrapper.get().getInputStream(), Message.class);
try
{
boolean isAuthenticatedHeader = false;
RabbitMq.rabbitMqLog(canonicalMessage);
isAuthenticatedHeader = authenticateAuthHeader(request.getHeader(Constants.AUTH), request.getHeader(Constants.PASS));
if (isAuthenticatedHeader)
{
Logger.logInfo(new LogDetails(CLASSNAME), "Aunthentication successful !!!! ");
if (Filter.isInValid().test(canonicalMessage))
{
throw new BadRequestException();
}
}
else
{
Logger.logError(new LogDetails(CLASSNAME), " Aunthentication not successful. Please check. ");
throw new NotAuthorizedException("Unauthorized Access");
}
Map<String, String> headers = Collections.list(((HttpServletRequest) request).getHeaderNames()).stream().collect(Collectors.toMap(h -> h, request::getHeader));
headers.updateHeaders(headers);
filterChain.doFilter(httpRequestWrapper.get(), response);
RabbitMq.rabbitMqLog();
}
catch (Exception e)
{}
}
}
tomcat启动后,我就处于异常之下:
2018-08-01 15:26:33,563 DEBUG [http-nio-8889-exec-1]
servlet.DispatcherServlet - Initializing servlet 'dispatcherServlet'
2018-08-01 15:26:33,564 INFO [http-nio-8889-exec-1] [localhost].[/]
- Initializing Spring FrameworkServlet 'dispatcherServlet' 2018-08-01 15:26:33,565 INFO [http-nio-8889-exec-1] servlet.DispatcherServlet -
FrameworkServlet 'dispatcherServlet': initialization started
2018-08-01 15:26:33,565 DEBUG [http-nio-8889-exec-1]
support.DefaultListableBeanFactory - Returning cached instance of
singleton bean 'multipartResolver' 2018-08-01 15:26:33,565 DEBUG
[http-nio-8889-exec-1] servlet.DispatcherServlet - Using
MultipartResolver
[org.springframework.web.multipart.support.StandardServletMultipartResolver@4f8969b0]
2018-08-01 15:26:33,566 DEBUG [http-nio-8889-exec-1]
support.DefaultListableBeanFactory - Creating instance of bean
'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver'
2018-08-01 15:26:33,566 DEBUG [http-nio-8889-exec-1]
support.DefaultListableBeanFactory - Returning cached instance of
singleton bean 'org.springframework.cache.config.internalCacheAdvisor'
2018-08-01 15:26:33,566 DEBUG [http-nio-8889-exec-1]
support.DefaultListableBeanFactory - Returning cached instance of
singleton bean
'org.springframework.transaction.config.internalTransactionAdvisor'
2018-08-01 15:26:33,571 DEBUG [http-nio-8889-exec-1]
support.DefaultListableBeanFactory - Returning cached instance of
singleton bean 'org.springframework.cache.config.internalCacheAdvisor'
2018-08-01 15:26:33,571 DEBUG [http-nio-8889-exec-1]
support.DefaultListableBeanFactory - Returning cached instance of
singleton bean
我尝试了以下操作:
if (request.getMethod().equals("POST") && (new AntPathRequestMatcher("/transactions/v1/enrichments").matches(request))) {
httpRequestWrapper = Optional.ofNullable(new HttpRequestWrapper(request));
if(httpRequestWrapper.isPresent())
{
CanonicalMessage canonicalMessage = objectMapper.readValue(httpRequestWrapper.get().getInputStream(), CanonicalMessage.class);
try
{
boolean isAuthenticatedHeader = false;
eventLogRawDataServiceImpl.eventLogExecution(canonicalMessage);
isAuthenticatedHeader = authenticateAuthHeader(request.getHeader(Constants.AUTHORIZATION), request.getHeader(Constants.DEPLOYMENT_TOKEN));
if (isAuthenticatedHeader)
{
ondotLogger.logInfo(new LogDetails(CLASSNAME), "Aunthentication successful !!!! ");
if (CloudServerFilter.isInValid().test(canonicalMessage))
{
throw new BadRequestException();
}
}
else
{
ondotLogger.logError(new LogDetails(CLASSNAME), " Aunthentication not successful. Please check. ");
throw new NotAuthorizedException("Unauthorized Access");
}
Map<String, String> headers = Collections.list(((HttpServletRequest) request).getHeaderNames()).stream().collect(Collectors.toMap(h -> h, request::getHeader));
cloudServerHeaders.updateHeaders(headers);
filterChain.doFilter(httpRequestWrapper.get(), response);
responseHandler.eventLoggerForResponse();
}
catch (Exception e)
{}
}
}else
{
filterChain.doFilter(request, response);
}
还有这个
@Configuration
public class CloudServerConfigAdapter extends WebMvcConfigurerAdapter{
@Autowired
private Filter filter;
@Bean
public FilterRegistrationBean myFilterRegistrationBean() {
FilterRegistrationBean regBean = new FilterRegistrationBean();
regBean.setFilter(filter);
regBean.setOrder(1);
regBean.addUrlPatterns("/transactions/v1/enrichments");
return regBean;
}
}
例外:
The Exception comes after wards :
018-08-01 15:26:33,612 DEBUG [http-nio-8889-exec-1] servlet.DispatcherServlet - Servlet 'dispatcherServlet' configured successfully
2018-08-01 15:26:33,628 DEBUG [http-nio-8889-exec-1] filter.OrderedRequestContextFilter - Bound request context to thread: org.apache.catalina.connector.RequestFacade@4e982e7d
2018-08-01 15:26:33,647 DEBUG [http-nio-8889-exec-1] filter.OrderedRequestContextFilter - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@4e982e7d
2018-08-01 15:26:33,648 ERROR [http-nio-8889-exec-1] [/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: com.ondot.cloudserver.handlers.HttpRequestWrapper$CachedServletInputStream@34378acb; line: 1, column: 0]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3854)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3799)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2924)
at com.ondot.cloudserver.handlers.CloudServerFilter.doFilterInternal(CloudServerFilter.java:57)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
2018-08-01 15:26:33,657 DEBUG [http-nio-8889-exec-1] servlet.DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-08-01 15:26:33,661 DEBUG [http-nio-8889-exec-1] annotation.RequestMappingHandlerMapping - Looking up handler method for path /error
2018-08-01 15:26:33,667 DEBUG [http-nio-8889-exec-1] annotation.RequestMappingHandlerMapping - Returning handler method [public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
2018-08-01 15:26:33,667 DEBUG [http-nio-8889-exec-1] support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'basicErrorController'
2018-08-01 15:26:33,667 DEBUG [http-nio-8889-exec-1] servlet.DispatcherServlet - Last-Modified value for [/error] is: -1
2018-08-01 15:26:33,762 DEBUG [http-nio-8889-exec-1] annotation.HttpEntityMethodProcessor - Written [{timestamp=Wed Aug 01 15:26:33 IST 2018, status=500, error=Internal Server Error, exception=com.fasterxml.jackson.databind.JsonMappingException, message=No content to map due to end-of-input
at [Source: com.ondot.cloudserver.handlers.HttpRequestWrapper$CachedServletInputStream@34378acb; line: 1, column: 0], path=/health/}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@6c6cb480]
2018-08-01 15:26:33,771 DEBUG [http-nio-8889-exec-1] servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-08-01 15:26:33,771 DEBUG [http-nio-8889-exec-1] servlet.DispatcherServlet - Successfully completed request
2018-08-01 15:26:34,274 DEBUG [http-nio-8889-exec-3] filter.OrderedRequestContextFilter - Bound request context to thread: org.apache.catalina.connector.RequestFacade@4e982e7d
2018-08-01 15:26:34,275 DEBUG [http-nio-8889-exec-3] filter.OrderedRequestContextFilter - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@4e982e7d
2018-08-01 15:26:34,275 ERROR [http-nio-8889-exec-3] [/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: com.ondot.cloudserver.handlers.HttpRequestWrapper$CachedServletInputStream@281c1fb0; line: 1, column: 0]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3854)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3799)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2924)
at com.ondot.cloudserver.handlers.CloudServerFilter.doFilterInternal(CloudServerFilter.java:57)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
2018-08-01 15:26:34,282 DEBUG [http-nio-8889-exec-3] servlet.DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-08-01 15:26:34,283 DEBUG [http-nio-8889-exec-3] annotation.RequestMappingHandlerMapping - Looking up handler method for path /error
2018-08-01 15:26:34,284 DEBUG [http-nio-8889-exec-3] annotation.RequestMappingHandlerMapping - Returning handler method [public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
2018-08-01 15:26:34,284 DEBUG [http-nio-8889-exec-3] support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'basicErrorController'
2018-08-01 15:26:34,284 DEBUG [http-nio-8889-exec-3] servlet.DispatcherServlet - Last-Modified value for [/error] is: -1
2018-08-01 15:26:34,289 DEBUG [http-nio-8889-exec-3] annotation.HttpEntityMethodProcessor - Written [{timestamp=Wed Aug 01 15:26:34 IST 2018, status=500, error=Internal Server Error, exception=com.fasterxml.jackson.databind.JsonMappingException, message=No content to map due to end-of-input
at [Source: com.ondot.cloudserver.handlers.HttpRequestWrapper$CachedServletInputStream@281c1fb0; line: 1, column: 0], path=/info}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@6c6cb480]
2018-08-01 15:26:34,289 DEBUG [http-nio-8889-exec-3] servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-08-01 15:26:34,289 DEBUG [http-nio-8889-exec-3] servlet.DispatcherServlet - Successfully completed request
2018-08-01 15:26:34,833 DEBUG [cloudServerDbConnectionPool housekeeper] pool.HikariPool - cloudServerDbConnectionPool - Before cleanup stats (total=21, active=0, idle=21, waiting=0)
2018-08-01 15:26:34,834 DEBUG [cloudServerDbConnectionPool housekeeper] pool.HikariPool - cloudServerDbConnectionPool - After cleanup stats (total=20, active=0, idle=20, waiting=0)
2018-08-01 15:26:34,834 DEBUG [cloudServerDbConnectionPool connection closer] pool.PoolBase - cloudServerDbConnectionPool - Closing connection oracle.jdbc.driver.T4CConnection@5ac8cbc1: (connection has passed idleTimeout)
2018-08-01 15:26:53,477 DEBUG [http-nio-8889-exec-2] filter.OrderedRequestContextFilter - Bound request context to thread: org.apache.catalina.connector.RequestFacade@4e982e7d
2018-08-01 15:26:53,478 DEBUG [http-nio-8889-exec-2] filter.OrderedRequestContextFilter - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@4e982e7d
2018-08-01 15:26:53,478 ERROR [http-nio-8889-exec-2] [/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: com.ondot.cloudserver.handlers.HttpRequestWrapper$CachedServletInputStream@76d11ae3; line: 1, column: 0]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3854)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3799)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2924)
at com.ondot.cloudserver.handlers.CloudServerFilter.doFilterInternal(CloudServerFilter.java:57)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
2018-08-01 15:26:53,480 DEBUG [http-nio-8889-exec-2] servlet.DispatcherServlet - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-08-01 15:26:53,480 DEBUG [http-nio-8889-exec-2] annotation.RequestMappingHandlerMapping - Looking up handler method for path /error
2018-08-01 15:26:53,481 DEBUG [http-nio-8889-exec-2] annotation.RequestMappingHandlerMapping - Returning handler method [public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
2018-08-01 15:26:53,481 DEBUG [http-nio-8889-exec-2] support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'basicErrorController'
2018-08-01 15:26:53,482 DEBUG [http-nio-8889-exec-2] servlet.DispatcherServlet - Last-Modified value for [/error] is: -1
2018-08-01 15:26:53,486 DEBUG [http-nio-8889-exec-2] annotation.HttpEntityMethodProcessor - Written [{timestamp=Wed Aug 01 15:26:53 IST 2018, status=500, error=Internal Server Error, exception=com.fasterxml.jackson.databind.JsonMappingException, message=No content to map due to end-of-input
at [Source: com.ondot.cloudserver.handlers.HttpRequestWrapper$CachedServletInputStream@76d11ae3; line: 1, column: 0], path=/health/}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@6c6cb480]
2018-08-01 15:26:53,487 DEBUG [http-nio-8889-exec-2] servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-08-01 15:26:53,487 DEBUG [http-nio-8889-exec-2] servlet.DispatcherServlet - Successfully completed request
2018-08-01 15:27:04,835 DEBUG [cloudServerDbConnectionPool housekeeper] pool.HikariPool - cloudServerDbConnectionPool - Pool stats (total=20, active=0, idle=20, waiting=0)
下面是我正在使用的包装器代码:
public class HttpRequestWrapper extends HttpServletRequestWrapper {
private ByteArrayOutputStream cachedBytes;
public HttpRequestWrapper(HttpServletRequest request) {
super(request);
}
@Override
public ServletInputStream getInputStream() throws IOException {
if (cachedBytes == null)
cacheInputStream();
return new CachedServletInputStream();
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
private void cacheInputStream() throws IOException {
/*Cache the inputstream in order to read it multiple times.*/
cachedBytes = new ByteArrayOutputStream();
IOUtils.copy(super.getInputStream(), cachedBytes);
}
final class CachedServletInputStream extends ServletInputStream {
private ByteArrayInputStream input;
public CachedServletInputStream() {
/* create a new input stream from the cached request body */
input = new ByteArrayInputStream(cachedBytes.toByteArray());
}
@Override
public int read() throws IOException {
return input.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener listener) {
}
}
}
它可以帮助我缓存请求并重新使用。以前,我使用拦截器来验证我的请求。但是我需要抓住我的请求,然后将其交给Rabbit MQ,因为我转移到了过滤器。