我试图应用自定义枚举注解上查询参数属性,我试图限制特定的值被接受进一步进行,但我得到500错误`与下面的细节.有人有任何线索吗?
//controller interface
@RestController @Validated @RequestMapping("/api/v1") public interface ReferenceDataController{
@GetMapping(value="/reference-data", produces= MediaType.APPLICATION_JSON)
@ResponseBody
public ResponseEntity<ReferenceData> getReferenceData(@QueryParam(value = "type of master data")@CustomMasterDataValidator String masterData,@NotNull @Valid @RequestHeader String correlationId);
}
//service impl
public class ReferenceDataServiceImpl implements ReferenceDataService {
@Override
public ResponseEntity<ReferenceData> getReferenceData(String masterData) {
try {
//TO-DO
} catch (final Exception e) {
log.error(" Exception while queuing request", e);
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ReferenceData(masterData,null,"500", "Internal Server Error", "UNK-001", e.getMessage()));
}
return ResponseEntity.status(HttpStatus.ACCEPTED).body(new ReferenceData(masterData,null,"200", "Accepted", null, null));
}}
//class to restrict specific value
@Target({ElementType.FIELD,ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented @Constraint(validatedBy = MasterDataValidator.class) public @interface CustomMasterDataValidator { public String message() default "Invalid Type for masterdate: IT should be voucherCode,productType,categoryCode,elementGroup,udas or masterSeasons" ;
Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {};
}
//class to define custom annotation
public class MasterDataValidator implements ConstraintValidator<CustomMasterDataValidator, String>{
@Override public boolean isValid(String value, ConstraintValidatorContext context) { return Arrays.asList("voucherCode","productType","categoryCode","elementGroup","udas","masterSeasons").contains(value); } }
而命中请求我得到500错误与丑陋的代码.
{
"errors": [
{
"systemErrorCode": 500,
"typeId": "SERVER_ERROR",
"message": "Request processing failed; nested exception is javax.validation.ConstraintDeclarationException: HV000151: A method overriding another method must not redefine the parameter constraint configuration, but method ReferenceDataControllerImpl#getReferenceData(String, String) redefines the configuration of ReferenceDataController#getReferenceData(String, String)."
}
]
}
Column A | Column B |
---|---|
Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
1条答案
按热度按时间hl0ma9xz1#
我不认为你可以在接口级别使用
@RestController
和@Validated
注解,因为它们是为类级别设计的:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/validation/annotation/Validated.html
如果你将它们移动到ReferenceDataServiceImpl,那么这个问题应该得到解决。实际上,将
@RequestMapping
移动到class级别也是合理的,除非你真的需要控制器的多个实现。(实际上,从Spring 5开始,可以在接口级别定义这个,但这并不意味着它总是有意义的)。P.S.即使它与您的错误无关,我也会在您的代码中进行以下改进:
@ResponseBody
注解(因为@RestController
已经包含它)ReferenceDataService
重命名为ReferenceDataController
,因为通常服务级别不是产生外部API的级别,而是从控制器级别调用的级别。