I'm working on a Spring Boot application where I need to create a Data Center using a REST API. However, I keep encountering an error stating "Name cannot be blank" even though I am providing the name
field in the request body.
@Slf4j@RestController@RequiredArgsConstructor@RequestMapping(AdminConstants.API_V1_DATA_CENTER_ROOT_URL)public class DataCenterRestApi { /** * Create data center. if user is not admin, it will be rejected. * * @param dataCenterDto data center dto * @return if the operation is success */ @Loggable @PreAuthorize(AUTHORIZE +" and hasRole('ADMIN')") @PostMapping public ResponseEntity<OperationStatus> createDataCenter(@RequestBody DataCenterDto dataCenterDto) { // Log incoming data Logger.getLogger(DataCenterRestApi.class.getName()) .info("Creating data center: " + dataCenterDto); if (Objects.isNull(dataCenterDto.getName())) { throw new IllegalArgumentException("Name cannot be blank"); } dataCenterService.createDataCenter(dataCenterDto); return ResponseEntity.ok(OperationStatus.SUCCESS); }}
And here is my DataCenterDto
class:
@Data@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)public class DataCenterDto extends BaseDto implements Serializable { @Serial private static final long serialVersionUID = -6342630857637389028L; @EqualsAndHashCode.Include private String publicId; @EqualsAndHashCode.Include @NotBlank(message = DataCenterConstants.BLANK_NAME) private String name; @Email(message = DataCenterConstants.INVALID_EMAIL) private String email; private String phone; // Address-related fields private String country; private String city; private String district; private String address; private String postalCode; // Other details private String description; private String ownerCompany; // Location-related fields @DecimalMin(value = DataCenterConstants.LATITUDE_MIN, message = DataCenterConstants.LATITUDE_RANGE) @DecimalMax(value = DataCenterConstants.LATITUDE_MAX, message = DataCenterConstants.LATITUDE_RANGE) private BigDecimal latitude; @DecimalMin(value = DataCenterConstants.LONGITUDE_MIN, message = DataCenterConstants.LONGITUDE_RANGE) @DecimalMax(value = DataCenterConstants.LONGITUDE_MAX, message = DataCenterConstants.LONGITUDE_RANGE) private BigDecimal longitude; // Activation status private boolean enabled;}
I am sending the following curl
request:
curl -X 'POST' \'http://localhost:8080/api/v1/datacenter' \ -H 'accept: application/hal+json' \ -H 'Authorization: Bearer YOUR_TOKEN_HERE' \ -H 'Content-Type: application/json' \ -d '{"id": 0,"version": 0,"createdAt": "2024-12-19T06:25:54.376Z","createdBy": "string","updatedAt": "2024-12-19T06:25:54.376Z","updatedBy": "string","publicId": "string","name": "Test Data Center","email": "test@gmail.com","phone": "12345678901","country": "Turkey","city": "Istanbul","district": "Besiktas","address": "Test Address","postalCode": "34020","description": "Test Description","ownerCompany": "Test Company","latitude": 90,"longitude": 180,"enabled": true}'
Despite providing the name
field, I receive the following error:
Name cannot be blank
2024-12-19T09:27:12.146+03:00 INFO 4016 --- [nio-8080-exec-2] c.ziyaret.annotation.impl.MethodLogger : => Starting - execution(DataCenterRestApi.createDataCenter(..)) args: [DataCenterDto(publicId=null, name=null, email=null, phone=null, country=null, city=null, district=null, address=null, postalCode=null, description=null, ownerCompany=null, latitude=null, longitude=null, enabled=false)]2024-12-19T09:27:12.150+03:00 INFO 4016 --- [nio-8080-exec-2] c.ziyaret.web.rest.v1.DataCenterRestApi : Creating data center: DataCenterDto(publicId=null, name=null, email=null, phone=null, country=null, city=null, district=null, address=null, postalCode=null, description=null, ownerCompany=null, latitude=null, longitude=null, enabled=false)2024-12-19T09:27:12.152+03:00 WARN 4016 --- [nio-8080-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [java.lang.IllegalArgumentException: Name cannot be blank]
I don't have data in the database. What could be causing this issue and how can I resolve it?
Any help or suggestions on resolving this issue would be greatly appreciated.