I am new to spring-boot I'm trying to create validation custom like below.
@ControllerAdvice
@RestController
public class SpecificResponse extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatus status, WebRequest request) {
logger.info("1");
List<String> errors = new ArrayList<String>();
// String coba = requestContext.getInfo();
for (FieldError error : ex.getBindingResult().getFieldErrors()) {
errors.add(error.getField() ": " error.getDefaultMessage());
}
for (ObjectError error : ex.getBindingResult().getGlobalErrors()) {
errors.add(error.getObjectName() ": " error.getDefaultMessage());
}
ApiError apiError = new ApiError();
apiError.setResponseCode("99");
apiError.setResponseDesc(ex.getBindingResult().getFieldValue("field") " Failed");
apiError.setResponseError(errors.toString());
return handleExceptionInternal(
ex, apiError, headers,HttpStatus.BAD_REQUEST, request);
}
}
Below is my DTO class
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.hibernate.annotations.NotFound;
import lombok.Data;
@Data
public class LiteTimeCreateDTO {
@NotNull
@NotEmpty(message = "Customer Code is required")
@NotFound
@NotBlank
private String customerCode;
@NotNull
@NotEmpty(message = "Customer Code2 is required")
@NotFound
@NotBlank
private String customerCode2;
@Email
private String customerCode3;
}
Below is my object sent by postman
{
// "customerCode": "as",
"customerCode3": "[email protected]",
"customerCode2" : "ds"
}
Below is my endpoint in controller
@GetMapping("")
public ResponseEntity<ApiSuccess> getData( @Valid @RequestBody(required = true) LiteTimeCreateDTO liteTimeCreateDTO, @RequestHeader(value = "User-Access") String header, BindingResult result){
ApiSuccess dataError = new ApiSuccess();
dataError.setResponseCode("00");
dataError.setResponseDesc("Get Lite Time Success");
// dataError.setResponseData(datas);
return new ResponseEntity<ApiSuccess>(dataError, HttpStatus.CREATED);
}
Everything is fine, it's just that I'm curious how to validate the sent dto is incomplete? when I send only 2 data there is no result response in my postman. How to fix and get the error response since there is no response in my postman.
CodePudding user response:
Do not use comments in JSON. Use the following request body instead:
{
"customerCode3": "[email protected]",
"customerCode2": "ds"
}
CodePudding user response:
The JSON is data only, and if you include a comment, then it will be data too.
You could have a designated data element called "_comment" (or something) that should be ignored by apps that use the JSON data.
You would probably be better having the comment in the processes that generates/receives the JSON, as they are supposed to know what the JSON data will be in advance, or at least the structure of it.