I work with a Java and Spring Boot project where I intend to make the basic setup. The GET
request for a single object it should return me JSON like this,
{
"productId": “string", // id of the requested product, e.g. "vegetable-123"
"requestTimestamp": “dateTime", // datetime in UTC when requested the stock
"stock": {
"id": "string",
"timestamp":
"dateTime" "quantity": "integer"
}
}
I make the models provided,
@Entity
public class Product {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name ="id")
private String id;
@Column(insertable = false, updatable = false)
private java.sql.Timestamp timestamp;
@Embedded
private Stock stock;
public Product() {
}
public Product(String id, Timestamp timestamp, Stock stock) {
this.id = id;
this.timestamp = timestamp;
this.stock = stock;
}
}
@Embeddable
public class Stock {
@Id
@Column(name = "stockId")
private String id;
@Column(name = "s_timestamp")
private java.sql.Timestamp timestamp;
@Column(name = "quantity")
private int quantity;
public Stock() {
}
public Stock(String id, Timestamp timestamp, int quantity) {
this.id = id;
this.timestamp = timestamp;
this.quantity = quantity;
}
}
The repository is provided below,
@Repository
public interface ProductRepository extends CrudRepository<Product, String>{
}
The service class is here,
@Service
public class ProductService {
@Autowired
private ProductRepository repository;
public Optional<Product> findById(String id) {
return repository.findById(id);
}
public List<Product> findAll() {
return (List<Product>) repository.findAll();
}
public Product save(Product product) {
return repository.save(product);
}
}
I tried to POST a product with the following API request,
@RestController
@RequestMapping("/api/v1/products")
public class ProductAPI {
@Autowired
private ProductService service;
@PostMapping(value = "/createProduct", consumes = "application/json", produces = "application/json")
public ResponseEntity<Product> createProduct(@RequestBody Product product) {
service.save(product);
return ResponseEntity.status(HttpStatus.CREATED).body(product);
}
}
I execute this via the cURL
,
$ curl -i -X POST -H "Content-Type:application/json" -d "{\"id\" : \"Apple Inc\",\"timestamp\" : \"2017-07-16T22:54:01.754Z\",\"stockId\" : \"Apple Stock\", \"s_timestamp\": \"2018-07-16T22:54:01.754Z\", \"quantity\": \"115\"}" http://localhost:8080/api/v1/products/createProduct
Immediately, I get the response via the terminal,
HTTP/1.1 500
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 14 Feb 2019 14:04:20 GMT
Connection: close
{"timestamp":"2019-02-14T14:04:20.482+0000","status":500,"error":"Internal Server Error","message":"could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement","path":"/api/v1/products/createProduct"}
The IDE is showing the message,
java.sql.SQLException: Field 'id' doesn't have a default value
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.15.jar:8.0.15]
How do I provide a default value to the id
?