spring-boot-logging icon indicating copy to clipboard operation
spring-boot-logging copied to clipboard

SpringResponseWrapper question ,response message loss on interceptor!

Open SYanalyser opened this issue 2 years ago • 3 comments

add interceptor the example, response message loss (no {"msg": "error"})

public class MainInterceptor implements AsyncHandlerInterceptor {
    final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "error");
        response.setStatus(200);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().append(objectMapper.writeValueAsString(map));
        return false;
    }
}

`@Configuration public class MainMvcConfig implements WebMvcConfigurer {

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new MainInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/req-param");
}

}`

>curl http://localhost:8080/

SYanalyser avatar Jun 29 '23 03:06 SYanalyser

I'm not very sure what's your question...

piomin avatar Oct 03 '23 07:10 piomin

HttpServletResponse -> loss data

SYanalyser avatar Oct 13 '23 03:10 SYanalyser

If I return a response in the interceptor, the data will be lost.@piomin

SYanalyser avatar Oct 13 '23 03:10 SYanalyser