spring boot RestController获取HttpServletResponse的内容

时间:2017-09-08 13:54:02

标签: spring-boot filter response httpresponse spring-restcontroller

我使用spring boot构建项目,RestController返回字符串数据。

我想在Filter中获取响应内容。

但是不能得到,请帮助我。

控制器:

@RestController
@RequestMapping(value = "/service/example")
public class ExampleController {
    @RequestMapping(value = "/get/test", method = RequestMethod.POST)
    public String message(@RequestBody String data) {
        return "test";
    }

    @RequestMapping(value = "/get/test1", method = RequestMethod.POST)
    public void message(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        writer.write("dfsfd");
        writer.flush();
    }
}

过滤器:

@WebFilter(filterName="myFilter",urlPatterns="/service/*")
public class MyFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
        MyHttpServletResponseWrapper responseWrapper = new MyHttpServletResponseWrapper(response);
        filterChain.doFilter(request, responseWrapper);
        String responseContent = responseWrapper.getContent();
        System.out.println("response="+responseContent);
    }
}

MyHttpServletResponseWrapper:

public class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {

    private PrintWriter cachedWriter;
    private CharArrayWriter bufferedWriter;

    /**
     * Constructs a response adaptor wrapping the given response.
     *
     * @param response The response to be wrapped
     * @throws IllegalArgumentException if the response is null
     */
    public MyHttpServletResponseWrapper(HttpServletResponse response) {
        super(response);
        bufferedWriter = new CharArrayWriter();
        cachedWriter = new PrintWriter(bufferedWriter);
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        return cachedWriter;
    }

    /**
     * 获取原始HTML
     *
     * @return
     */
    public String getContent() {
        byte[] bytes = bufferedWriter.toString().getBytes();
        try {
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }
}

发布到/ service / example / get / test无法获取内容。 但发布到/ service / example / get / test1可以获得内容。

为什么?

我的项目有很多休息,例如/ service / example / get / test,我不想改变每一个。

如何在过滤器中获取响应内容,请帮助,谢谢!!!

1 个答案:

答案 0 :(得分:2)

我创建了一个简单的spring boot项目,在这个项目中你可以控制你要过滤的网址:

休息服务类(3项服务,我们只过滤2)

import java.io.IOException;
import java.io.PrintWriter;

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

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.GenericFilterBean;

@SpringBootApplication
@RestController
@RequestMapping(value = "/service/example")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping(value = "/get/test", method = RequestMethod.POST)
    public String message(@RequestBody String data) {
        return "test";
    }

    @RequestMapping(value = "/get/test1", method = RequestMethod.POST)
    public void message(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        writer.write("dfsfd");
        writer.flush();
    }

    @RequestMapping(value = "/api", method = RequestMethod.POST)
    public void messages(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        writer.write("dfsfd");
        writer.flush();
    }


    @Bean
    public FilterRegistrationBean someFilterRegistration() {

        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(myFilter());
        registration.addUrlPatterns("/service/example/get/*");
        registration.setOrder(1);
        return registration;
    } 

    @Bean(name = "someFilter")
    public GenericFilterBean myFilter() {
        return new MyFilter();
    }
}

MyFilter类:

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.web.filter.GenericFilterBean;

public class MyFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        System.out.println("Filter called");
        filterChain.doFilter(servletRequest, servletResponse);
    }

}

尝试拨打3项服务:

http://localhost:8080/service/example/get/test

http://localhost:8080/service/example/get/test1

http://localhost:8080/service/example/api

并检查打印的日志。

您可以使用此行控制网址格式

registration.addUrlPatterns("/service/example/get/*");

我希望此示例可以帮助您,谢谢