我有一个使用Spring和Axon的CQRS Rest API。使用javax.validation
库为输入设置验证。验证工作正常,并捕获'用户名'需要至少2个字符长。但是,与失败验证相关联的消息没有显示在对Post请求的响应中。
响应最终缺少"错误"one_answers"消息"信息,我不知道为什么。难道MethodArgumentNotValidException
不会被推到try-catch块并被处理吗?如果验证工作正常,为什么消息没有发出'400'状态响应?
我错过了什么?
这是我想要得到的响应:
{
"timestamp": "2022-04-24T01:23:27.809+00:00",
"status": 400,
"error": "Bad Request",
"errors": [
{
"codes": [
"Size.registerUserCommand.user.account.username",
"Size.user.account.username",
"Size.username",
"Size.java.lang.String",
"Size"
],
"arguments": [
{
"codes": [
"registerUserCommand.user.account.username",
"user.account.username"
],
"arguments": null,
"defaultMessage": "user.account.username",
"code": "user.account.username"
},
2134838498,
2
],
"defaultMessage": "username must have a minimum of 2 characters",
"objectName": "registerUserCommand",
"field": "user.account.username",
"rejectedValue": "m",
"bindingFailure": false,
"code": "Size
}
],
"message": "Validation failed for object='registerUserCommand'. Error count: 1",
"path": "/api/v1/registerUser"
}
这是我实际得到的响应:
{
"timestamp": "2022-04-24T01:23:27.809+00:00",
"status": 400,
"error": "Bad Request",
"message": "",
"path": "/api/v1/registerUser"
}
发送到控制器的Post请求:
{
"user": {
"firstname": "Mike",
"lastname": "Jacobs",
"emailAddress": "mike@spripgbank.com",
"account": {
"username": "m",
"password": "mik959593e0",
"roles": [
"READ_PRIVILEGE"
]
}
}
}
Post请求的控制台输出:
2022-04-24 20:32:21.339 INFO 11040 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-04-24 20:32:21.340 INFO 11040 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-04-24 20:32:21.343 INFO 11040 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
2022-04-24 20:32:22.120 WARN 11040 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public org.springframework.http.ResponseEntity<com.springbank.user.cmd.api.dto.RegisterUserResponse> com.springbank.user.cmd.api.controllers.RegisterUserController.registerUser(com.springbank.user.cmd.api.commands.RegisterUserCommand): [Field error in object 'registerUserCommand' on field 'user.account.username': rejected value [m]; codes [Size.registerUserCommand.user.account.username,Size.user.account.username,Size.username,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [registerUserCommand.user.account.username,user.account.username]; arguments []; default message [user.account.username],2147483647,2]; default message [username must have a min of 2 characters]] ]
接受请求的控制器:
package com.springbank.user.cmd.api.controllers;
import com.springbank.user.cmd.api.commands.RegisterUserCommand;
import com.springbank.user.cmd.api.dto.RegisterUserResponse;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.UUID;
@RestController
@RequestMapping(path = "/api/v1/registerUser")
public class RegisterUserController {
private final CommandGateway commandGateway;
@Autowired
public RegisterUserController(CommandGateway commandGateway) {
this.commandGateway = commandGateway;
}
@PostMapping
public ResponseEntity<RegisterUserResponse> registerUser(@Valid @RequestBody RegisterUserCommand command) {
var id = UUID.randomUUID().toString();
command.setId(id);
try{
commandGateway.send(command);
return new ResponseEntity<>(new RegisterUserResponse( id, "User Successfully Registered"), HttpStatus.CREATED);
}catch (Exception e) {
var safeErrorMessage = "Error while processing register user request for id - " + id;
System.out.println(e.toString());
return new ResponseEntity<>(new RegisterUserResponse( id, safeErrorMessage), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
命令:
package com.springbank.user.cmd.api.commands;
import com.springbank.user.core.models.User;
import lombok.Builder;
import lombok.Data;
import org.axonframework.modelling.command.TargetAggregateIdentifier;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
@Data
@Builder
public class RegisterUserCommand {
@TargetAggregateIdentifier
private String id;
@NotNull(message = "Need to supply User info")
@Valid
private User user;
}
用户模型:
package com.springbank.user.core.models;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Document(collection = "users")
public class User {
@Id
private String id;
@NotEmpty(message = "firstname is mandatory")
private String firstname;
@NotEmpty(message = "lastname is mandatory")
private String lastname;
@Email(message = "please provide a valid email")
private String emailAddress;
@NotNull(message = "Please provide account Credentials")
@Valid
private Account account;
}
正在进行验证的帐户模型:
package com.springbank.user.core.models;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Account {
@Size(min = 2, message = "username must have a min of 2 characters")
private String username;
@Size(min = 7, message = "password must have a min of 7 characters")
private String password;
@NotNull(message = "specify at least one user role")
private List<Role> roles;
}
RegisterUserResponse:
package com.springbank.user.cmd.api.dto;
public class RegisterUserResponse extends BaseResponse{
private String id;
public RegisterUserResponse(String id, String message) {
super(message);
this.id = id;
}
}
BasicResponse:
package com.springbank.user.cmd.api.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class BaseResponse {
private String Message;
}
由于您用@Valid RegisterUserCommand
注释了控制器方法参数,因此对象的验证将在执行进入try/catch块之前进行。
如果您查看位于ResponseEntityExceptionHandler中的以下方法然后,您将看到默认实现是这样的。正如您所看到的,默认情况是发送null作为body,这会导致遗漏细节。
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
return handleExceptionInternal(ex, null, headers, status, request);
}
因此,如果您想以不同的方式处理该错误,则需要为该异常类型添加自定义异常处理,并覆盖默认处理程序。