Response body is empty when using DeferredResult

2019-09-08 15:53发布

问题:

Response body is empty when using DeferredResult with Java Config.

Controller:

@Controller
public class HomeController {

    @RequestMapping("/")
    public DeferredResult<ModelAndView> home() {
        final DeferredResult<ModelAndView> result = new DeferredResult<>();

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                result.setResult(new ModelAndView("home", Collections.singletonMap("name", "World")));
            }
        }, 2);

        return result;
    }
}

Configuration:

@Configuration
@ComponentScan(basePackages = "to.talk.gzip.test")
@PropertySource(value = "classpath:/application.properties")
@EnableWebMvc
public class AppConfig {

    @Bean
    public ViewResolver mustacheViewResolver() {
        MustacheViewResolver resolver = new MustacheViewResolver();
        resolver.setPrefix("views/");
        resolver.setSuffix(".html");
        return resolver;
    }
}

回答1:

This seems to be a bug in Jetty 9. It works with Jetty 8.

Reported an issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=408117



回答2:

Does it work without using a DeferredResult? I.e.

@RequestMapping("/")
public ModelAndView home() {
    return new ModelAndView("home", Collections.singletonMap("name", "World"));
}

If that doesn't work, it may be a view related issue. If it does, then enable debug logging on or.springframework.web and look at the output for more clues. And/or add some logging of your own.