繁体   English   中英

从Spring拦截器打印时HTTP状态错误(0)-在独立TOMCAT中将应用程序部署为WAR时

[英]Wrong HTTP Status (0) when printing it from a Spring interceptor - When application is deployed as WAR in standalone TOMCAT

我想使用拦截器将HTTP请求记录到RESTful Web服务。 我使用的是1.1.5.RELEASE版本的spring-boot。

这是我的拦截器:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ModelAndViewMethodReturnValueHandler;

public class WebRequestsInterceptor extends HandlerInterceptorAdapter {

    private static final Logger logger = LoggerFactory
            .getLogger(WebRequestsInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        long startTime = System.currentTimeMillis();
        request.setAttribute("startTime", startTime);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {

        long startTime = (Long) request.getAttribute("startTime");
        long endTime = System.currentTimeMillis();
        long executeTime = endTime - startTime;
        logger.info(request.getMethod() + " " + request.getRequestURL() + "?"
                + request.getQueryString() + " Http Status "
                + response.getStatus() + " elapsed time : " + executeTime
                + "ms");

    }

}

添加拦截器的类:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new WebRequestsInterceptor());
    }

}

我遇到的问题是,当我在独立的Tomcat(v。7.0.50)中部署打包为war的应用程序时,我尝试记录的HTTP状态始终为0。 当我运行带有嵌入式tomcat(由spring-boot提供)的应用程序时,HTTP状态正确。

在调试模式下,当我检查响应时,可以看到以某种方式发生的错误:org.springframework.boot.context.web.ErrorPageFilter$ErrorWrapperResponse@4456be6c并且状态为0,实际上。

最后,我不得不使用一个方面来记录对Web服务的访问,但是拦截器方法也应该起作用。

有人遇到过同样的问题吗? 谢谢。

编辑

一个虚拟的rest控制器,我要为其拦截请求:

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello-world")
public class HelloWorldController {

    private static final String template = "Hello, %s!";

    private final AtomicLong counter = new AtomicLong();

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity<Greeting> sayHello(
            @RequestParam(value = "name", required = false, defaultValue = "Stranger")
            String name) {

        switch (name) {
        case "test":
            return new ResponseEntity<>(new Greeting(counter.incrementAndGet(),
                    String.format(template, name)), HttpStatus.FORBIDDEN);
        case "name":
            return new ResponseEntity<>(new Greeting(counter.incrementAndGet(),
                    String.format(template, name)), HttpStatus.NOT_FOUND);
        case "spring":
            return new ResponseEntity<>(new Greeting(counter.incrementAndGet(),
                    String.format(template, name)), HttpStatus.OK);
        default:
            return new ResponseEntity<>(new Greeting(counter.incrementAndGet(),
                    String.format(template, name)), HttpStatus.BAD_REQUEST);
        }

    }

}

已在此处修复: https//github.com/spring-projects/spring-boot/commit/2fae4afe (即版本1.1.6)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM