使用spring-validations:在包含@validate和validation注释的restful服务中未返回



Rest-service:Spring-boot、Postman、Tomcat、maven是我用来验证数据的技术和工具:我希望在poster响应中有一些消息。你能告诉我我必须在下面的代码中确定哪些更改吗。

@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]

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

相关内容

  • 没有找到相关文章

最新更新