I have a Spring Boot
based REST API
that supports CRUD
operations on a resource. All the methods are working fine except POST where I see two problems:
- The
Response
section (mentioned below) keepsactive
value asnull
even though the DB has the default value of 'Y' defined inschema.sql
- The sequence is generating negative value (-45) even though the sequence is supposed to start with 1 and increment by as defined in
schema.sql
Endpoint:
http://localhost:8080/client (POST)
Payload:
{
"clientName": "Walmart"
}
Response: (Why is active field null and id=-45 instead of 4 here???)
{
"clientName": "Walmart",
"active": null, //THIS SHOULD BE 'Y'
"_links": {
"self": {
"href": "http://localhost:8080/client/-45" (THIS SHOULD BE 4 INSTEAD OF -45)
},
"client": {
"href": "http://localhost:8080/client/-45"
}
}
}
schema.sql
CREATE SEQUENCE IF NOT EXISTS CLIENT_SEQ START WITH 1 INCREMENT BY 1;
CREATE TABLE IF NOT EXISTS CLIENT(
CLIENT_ID BIGINT NOT NULL DEFAULT CLIENT_SEQ.NEXTVAL PRIMARY KEY,
CLIENT_NAME VARCHAR(255) NOT NULL,
ACTIVE CHAR(1) NOT NULL DEFAULT 'Y'
);
data.sql
INSERT INTO CLIENT(CLIENT_NAME) VALUES ('SPOTIFY');
INSERT INTO CLIENT(CLIENT_NAME) VALUES ('DAILY BURN');
INSERT INTO CLIENT(CLIENT_NAME) VALUES ('CREATIVE BUG');
ClientRepository.java
@RepositoryRestResource(path = "client")
public interface ClientRepository extends PagingAndSortingRepository<Client, Long> {
}
Client.java
@Entity
@Data
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "client_id_generator")
@SequenceGenerator(name = "client_id_generator", sequenceName = "client_seq")
private Long clientId;
private String clientName;
private String active;
}