Java 如何验证rest路径(spring引导)
如何验证从以下URL或任何类似URL提供的路径变量(storeId、customerId、accountId)Java 如何验证rest路径(spring引导),java,spring,spring-boot,restful-authentication,Java,Spring,Spring Boot,Restful Authentication,如何验证从以下URL或任何类似URL提供的路径变量(storeId、customerId、accountId) /store/{storeId}/customers/{customerId}/accounts/{accountId} 如果用户开始编写随机存储ID/customerID,并尝试创建以下资源 POST/store/478489/customers/56423/accounts(假设478489和56423没有指向有效的资源)。 我想返回正确的错误代码,例如HttpStatus.NOT
/store/{storeId}/customers/{customerId}/accounts/{accountId}
如果用户开始编写随机存储ID/customerID
,并尝试创建以下资源
POST/store/478489/customers/56423/accounts
(假设478489和56423没有指向有效的资源)。
我想返回正确的错误代码,例如HttpStatus.NOT\u FOUND,HttpStatus.BAD\u REQUEST
我正在使用Java和spring boot
下面的问题更详细地解释了我的问题,但没有太多的回答。
从提供的URL
/store/{storeId}/customers/{customerId}/accounts/{accountId}
,
很明显,商店有顾客
,而那些顾客有账户
以下方法包括额外的数据库调用,用于按ID验证store和按ID验证customer,但这将是适当的方法,因为如果我们对store和customer表使用带有联接的查询,则您可能无法准确判断给定的storeId或customerId是否不正确/不在数据库中
如果您一步一步地进行,您可以显示相应的错误消息
如果storeId不正确-不存在具有给定storeId:XYZ的存储
如果customerId不正确-不存在customerId为XYZ的客户
因为您提到您正在使用Spring Boot,所以您的代码应该如下所示:
@RequestMapping(value = "/store/{storeId}/customers/{customerId}/accounts",
method = RequestMethod.POST)
public ResponseEntity<Account> persistAccount(@RequestBody Account account, @PathVariable("storeId") Integer storeId,
@PathVariable("customerId") Integer customerId) {
// Assuming you have some service class @Autowired that will query store by ID.
// Assuming you have classes like Store, Customer, Account defined
Store store = service.getStoreById(storeId);
if(store==null){
//Throw your exception / Use some Exception handling mechanism like @ExceptionHandler etc.
//Along with proper Http Status code.
//Message will be something like: *There exists no store with given storeId: XYZ*
}
Customer customer = service.getAccountById(storeId, customerId);
if(customer==null){
//Throw your exception with proper message.
//Message will be something like: *There exists no store with given customerID: XYZ*
}
// Assuming you already have some code to save account info in database.
// for convenience I am naming it as saveAccountAgainstStoreAndCustomer
Account account = service.saveAccountAgainstStoreAndCustomer(storeId, customerId, account);
ResponseEntity<Account> responseEntity = new ResponseEntity<Account>(account, HttpStatus.CREATED);
}
@RequestMapping(value=“/store/{storeId}/customers/{customerId}/accounts”,
method=RequestMethod.POST)
public ResponseEntity persistAccount(@RequestBody Account,@PathVariable(“storeId”)Integer storeId,
@PathVariable(“customerId”)整数(customerId){
//假设您有一些服务类@Autowired,它将按ID查询存储。
//假设您定义了像Store、Customer和Account这样的类
Store Store=service.getStoreById(storeId);
if(store==null){
//抛出异常/使用一些异常处理机制,如@ExceptionHandler等。
//以及正确的Http状态代码。
//消息类似:*不存在具有给定storeId:XYZ的存储*
}
Customer=service.getAccountById(storeId,customerId);
如果(客户==null){
//抛出带有正确消息的异常。
//消息类似:*不存在具有给定customerID:XYZ的商店*
}
//假设您已经有一些代码将帐户信息保存在数据库中。
//为了方便起见,我将其命名为saveAccountAgainstStoreAndCustomer
Account Account=service.saveAccountAgainstStoreAndCustomer(storeId、customerId、Account);
ResponseEntity ResponseEntity=新的ResponseEntity(帐户,HttpStatus.CREATED);
}
上面的代码片段只是您的代码应该是什么样子的一个框架,通过遵循一些良好的编码实践,您可以以比上面给出的更好的方式来构建它
我希望它能有所帮助。从提供的URL/store/{storeId}/customers/{customerId}/accounts/{accountId}
,
很明显,商店有顾客
,而那些顾客有账户
以下方法包括额外的数据库调用,用于按ID验证store和按ID验证customer,但这将是适当的方法,因为如果我们对store和customer表使用带有联接的查询,则您可能无法准确判断给定的storeId或customerId是否不正确/不在数据库中
如果您一步一步地进行,您可以显示相应的错误消息
如果storeId不正确-不存在具有给定storeId:XYZ的存储
如果customerId不正确-不存在customerId为XYZ的客户
因为您提到您正在使用Spring Boot,所以您的代码应该如下所示:
@RequestMapping(value = "/store/{storeId}/customers/{customerId}/accounts",
method = RequestMethod.POST)
public ResponseEntity<Account> persistAccount(@RequestBody Account account, @PathVariable("storeId") Integer storeId,
@PathVariable("customerId") Integer customerId) {
// Assuming you have some service class @Autowired that will query store by ID.
// Assuming you have classes like Store, Customer, Account defined
Store store = service.getStoreById(storeId);
if(store==null){
//Throw your exception / Use some Exception handling mechanism like @ExceptionHandler etc.
//Along with proper Http Status code.
//Message will be something like: *There exists no store with given storeId: XYZ*
}
Customer customer = service.getAccountById(storeId, customerId);
if(customer==null){
//Throw your exception with proper message.
//Message will be something like: *There exists no store with given customerID: XYZ*
}
// Assuming you already have some code to save account info in database.
// for convenience I am naming it as saveAccountAgainstStoreAndCustomer
Account account = service.saveAccountAgainstStoreAndCustomer(storeId, customerId, account);
ResponseEntity<Account> responseEntity = new ResponseEntity<Account>(account, HttpStatus.CREATED);
}
@RequestMapping(value=“/store/{storeId}/customers/{customerId}/accounts”,
method=RequestMethod.POST)
public ResponseEntity persistAccount(@RequestBody Account,@PathVariable(“storeId”)Integer storeId,
@PathVariable(“customerId”)整数(customerId){
//假设您有一些服务类@Autowired,它将按ID查询存储。
//假设您定义了像Store、Customer和Account这样的类
Store Store=service.getStoreById(storeId);
if(store==null){
//抛出异常/使用一些异常处理机制,如@ExceptionHandler等。
//以及正确的Http状态代码。
//消息类似:*不存在具有给定storeId:XYZ的存储*
}
Customer=service.getAccountById(storeId,customerId);
如果(客户==null){
//抛出带有正确消息的异常。
//消息类似:*不存在具有给定customerID:XYZ的商店*
}
//假设您已经有一些代码将帐户信息保存在数据库中。
//为了方便起见,我将其命名为saveAccountAgainstStoreAndCustomer
Account Account=service.saveAccountAgainstStoreAndCustomer(storeId、customerId、Account);
ResponseEntity ResponseEntity=新的ResponseEntity(帐户,HttpStatus.CREATED);
}
上面的代码片段只是您的代码应该是什么样子的一个框架,通过遵循一些良好的编码实践,您可以以比上面给出的更好的方式来构建它
我希望这会有所帮助。检查资源是否存在,如果不存在,则抛出异常,在ControllerAdvice中捕获该异常,并将其映射到http状态码,然后返回给调用客户端。因此,我应该检查每个id,如果每个id都只存在,然后执行请求的操作(put、post、get)?好的,你需要验证每一个,所以你把它们都拿出来,在数据库中查找,然后