I am trying to save data using spring boot mysql. Every call gives 200 altough there is no data in the GET response.
application.properties
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/smyc
spring.datasource.username=root
spring.datasource.password=root
spring.sql.init.schema-locations=classpath:sql/schema.sql
spring.sql.init.mode=always
UserController.kt
@RestController
@RequestMapping("/users")
class UserController(val service: UserService) {
@GetMapping
fun getAllUsers(): List<User> = service.getAllUsers()
@PostMapping(
"/add",
consumes = [MediaType.APPLICATION_JSON_VALUE]
)
fun addUser(@RequestBody user: User) {
service.addUser(user)
}
}
UserRepository.kt
interface UserRepository : CrudRepository<User, String> {
@Query("SELECT * FROM users")
fun getAllUsers(): List<User>
}
User.kt
@Table("users")
data class User(
val id: String,
val firstName: String,
val lastName: String,
val email: String,
val password: String,
val phone: String,
)
CodePudding user response:
Update User.kt so that it contains
@Entity
(at class level) and @Id
(at id level)
@Table("users")
@Entity
data class User(
@Id
val id: String,
val firstName: String,
val lastName: String,
val email: String,
val password: String,
val phone: String,
)
CodePudding user response:
You can use save method:
MerchandiseEntity pants = new MerchandiseEntity( "Pair of Pants", 34.99);
pants = repo.save(pants);