首页 > 解决方案 > 如何在 Spring Boot 中调试 REST 调用的性能问题

问题描述

Spring 框架的优点和缺点在于注释背后发生了很多神奇的事情。我已经构建了一个我认为公平的东西vanilla REST api,它使GET、PUT 和 POST都在同一个上URL。当调用PUTor调用时,在我从Postman发送查询到控制器方法获得控制权之间POST有一个严重的时间(8 到 10 秒)。然而,呼叫几乎是瞬间的。在调用控制器方法之前,我需要一种方法来追踪魔术以查看性能瓶颈在哪里。如何调试此代码?delayGETspring

@RequestMapping(value = "/{collection}/branches", method = RequestMethod.GET)
public ResponseEntity<CollectionApi<BranchApi>> getBranches(@PathVariable String collection,
        @RequestParam(defaultValue = ModelsController.DEF_OFFSET) Long _offset,
        @RequestParam(defaultValue = ModelsController.DEF_LIMIT) Integer _limit,
        @RequestParam(required = false) String rep,
        @RequestParam(defaultValue = ModelsController.DEF_SYNC) Integer _sync) {

    try {
        CollectionApi<BranchApi> branches = branchesService.getBranches(collection);
        if (branches == null) {
            throw new NotFoundException("Collection not found");
        }
        return ResponseEntity.ok(branches);
    } catch (NucleusModelingException e) {
        throw new BadRequestException(e.getLocalizedMessage(), e);
    }
}

@RequestMapping(value = "/{collection}/branches", method = RequestMethod.PUT)
public ResponseEntity<CollectionApi<BranchApi>> setActiveBranch(
        @PathVariable String collection, 
        @RequestParam String branch) {

    try {
        CollectionApi<BranchApi> branches = branchesService.setActiveBranch(collection, branch);
        return ResponseEntity.ok(branches);
    } catch (NucleusModelingException e) {
        throw new BadRequestException(e.getLocalizedMessage(),e);
    }
}

@RequestMapping(value = "/{collection}/branches", method = RequestMethod.POST)
public ResponseEntity<CollectionApi<BranchApi>> createBranch(
        @PathVariable String collection, 
        @RequestParam String branch) {

    try {
        CollectionApi<BranchApi> branches = branchesService.checkoutBranch(collection, branch);
        return ResponseEntity.status(HttpStatus.CREATED).body(branches);
    } catch (NucleusModelingException e) {
        throw new BadRequestException(e.getLocalizedMessage(),e);
    }
}

标签: javaspringdebuggingspring-bootperformance-testing

解决方案


推荐阅读