【问题标题】:Using spring validations : Message not been returned in restful service containing @valid and validation annotations使用spring验证:在包含@valid和验证注释的restful服务中没有返回消息
【发布时间】:2020-10-24 15:26:20
【问题描述】:

Rest 服务:Spring boot、Postman、Tomcat、maven 是我用来验证数据的技术和工具:我期待 postman 响应中的一些消息。您能否建议我在下面的代码中必须确保的更改是什么。

    @RestController
    @RequestMapping("/requestShipment")
    @Validated
    public class ShipmentRequestController {
    
        @GetMapping
        public String requestShipment(@Valid @RequestBody  ShipmentRequest shipmentRequest) {
            System.out.println("helloo");
            return "Sampleclass";
        }
        
        /*
         * @PostMapping(value="/sampleclass") public @ResponseBody void
         * requestSampleClass(@Valid @RequestBody SampleClass sampleclass) {
         * 
         * }
         */
        
        @GetMapping(value="/sampleclass")
        public String requestSampleClass(@Valid @PathParam("data") @NotNull String data) {
            return data;
        }
    }
    
    Postman request : (according to the request, last method will be called)
    http://localhost:8080/requestShipment/sampleclass
    Result I am getting from postman :
    
    {
        "timestamp": "2020-10-24T06:27:07.376+00:00",
        "status": 500,
        "error": "Internal Server Error",
        "message": "",
        "path": "/requestShipment/sampleclass"
    }
    
if you could observe in above response,  message attribute is null
Expected result : message had to be written. something like " requestSampleClass.data: must not be null"
    
    Error in the Spring boot console :
    javax.validation.ConstraintViolationException: requestSampleClass.data: must not be null
        at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:116) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at com.rakuten.shipment.controller.ShipmentRequestController$$EnhancerBySpringCGLIB$$e373f1a4.requestSampleClass(<generated>) ~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]

【问题讨论】:

    标签: java spring-boot rest validation microservices


    【解决方案1】:

    @PathParam 不是有效的 Spring 注释,因此 Spring 没有填充该值。我不确定您要添加什么依赖项来提供它,但您可能正在混合系统。 (你在路径中没有任何占位符,所以它根本没有意义。)我认为你想要@RequestParam

    【讨论】:

    • 您提到“路径中没有占位符”:这就是我验证数据而不在 URL 中传递任何内容的原因。 (来自邮递员)。什么依赖: org.springframework.bootspring-boot-starter-validation @RequestParam:即使我保持不变,它也不起作用..让我知道你是否有其他想法。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    相关资源
    最近更新 更多