处理REST API调用的顺序

我有一个问题要问。我已经创建了Service调用的同步Controller

@Controller
public class WebAppApiController {
    private final WebAppService webApService;
    @Autowired
    WebAppApiController(WebAppService webApService){
        this.webApService= webApService;

    }
    @Transactional
    @PreAuthorize("hasAuthority('ROLE_API')")
    @PostMapping(value = "/api/webapp/{projectId}")
    public ResponseEntity<Status> getWebApp(@PathVariable(value = "projectId") Long id,@RequestBody WebAppRequestModel req) {
        return webApService.processWebAppRequest(id,req);
    }
}

Service层只是检查请求中是否没有重复项并将其存储在数据库中。因为使用该端点的客户端连续发出许多请求,所以在一个请求被验证之前,没有重复的其他请求被放入数据库中,这就是为什么我试图进行同步阻止。

@Service
public class WebAppService {
    private final static String UUID_PATTERN_TO = "[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}";
    private final WebAppRepository waRepository;
    @Autowired
    public WebAppService(WebAppRepository waRepository){
        this.waRepository= waRepository;
    }

    @Transactional(rollbackOn = Exception.class)
    public ResponseEntity<Status> processScanWebAppRequest(Long id,WebAppScanmodel webAppScanmodel){
        try{
            synchronized (this){
                Optional<WebApp> webApp=verifyForDuplicates(webAppScanmodel);
                if(!webApp.isPresent()){
                    WebApp webApp=new WebApp(webAppScanmodel.getUrl())
                    webApp=waRepository.save(webApp);
                    processpropertiesOfWebApp(webApp);
                    return new ResonseEntity<>(HttpStatus.CREATED);
                }
                return new ResonseEntity<>(HttpStatus.CONFLICT);
            }
        } catch (NonUniqueResultException ex){
            return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
        } catch (IncorrectResultSizeDataaccessException ex){
            return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
        }
    }
}
Optional<WebApp> verifyForDuplicates(WebAppScanmodel webAppScanmodel){
    return waRepository.getWebAppByRegex(webAppScanmodel.getUrl().replaceAll(UUID_PATTERN_TO,UUID_PATTERN_TO)+"$");
}

和JPA方法:

@Query(value="select * from webapp wa where wa.url ~ :url",nativeQuery = true)
Optional<WebApp> getWebAppByRegex(@Param("url") String url);

processpropertiesOfWebApp方法正在对给定的webapp进行进一步处理,这时应该是唯一的。

预期的行为是: 当客户发布请求包含多个网址时:

https://testdomain.com/user/7e1c44e4-821b-4d05-bdc3-ebd43dfeae5f
https://testdomain.com/user/d398316e-fd60-45a3-b036-6d55049b44d8
https://testdomain.com/user/c604b551-101f-44c4-9eeb-d9adca2b2fe9

只有第一个存储在数据库中,但是目前还没有发生。从我的数据库中选择:

select inserted,url from webapp where url ~ 'https://testdomain.com/users/[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12}$';
2019-11-07 08:53:05 | https://testdomain.com/users/d398316e-fd60-45a3-b036-6d55049b44d8
2019-11-07 08:53:05 | https://testdomain.com/users/d398316e-fd60-45a3-b036-6d55049b44d8
2019-11-07 08:53:05 | https://testdomain.com/users/d398316e-fd60-45a3-b036-6d55049b44d8 
(3 rows)

我将尝试在unique列上添加url约束,但是当UUID更改为新的url为{{ 1}}

谁能给我一个提示我做错了什么?

问题与the one I asked before有关,但找不到合适的解决方案,因此我简化了方法,但仍然没有成功

liyazhao 回答:处理REST API调用的顺序

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3146519.html

大家都在问