Spring Boot REST API请求超时

时间:2018-04-02 03:30:53

标签: spring rest api spring-boot

我有一个简单的spring-boot应用程序,它与mongodb连接并公开数据。这是我的控制器。

@RequestMapping(value = "/{coll}", method = RequestMethod.GET)
    public List<Map> retrieveMongoData(@PathVariable("collection") String collection ,
                                       @RequestParam Map<String,String> requestParams) throws Exception{

        Query query = new Query();  
        ........  
        return mongoTemplate.find(query, Map.class, collection);
    }

现在我需要向REST API添加请求超时。 Here解释了返回Callable的解决方案。

在我的情况下,我在控制器中返回List<Map>。如何在遵守控制器的同时实现超时功能。

1 个答案:

答案 0 :(得分:0)

通过关注您提供的链接,您可以将请求的处理方式从同步更改为异步,这可以解决您的问题,但不要忘记这会带来新的开销(如果您的请求实际上不需要异步)处理)。我认为更好的方法是直接使用连接超时配置嵌入式tomcat,所以我想通过向server.tomcat.connection-timeout=20000文件添加.properties,这将使请求的最大时间为20秒。