Reputation: 147
I send a request to the rest-api to store a object into mysql database.
Which step is missing that i can store via Jpa the objects into my database?
Here is the Rest-Controller
@RestController
public class OwnerRestController {
@Autowired
private final OwnerRestRepository repo;
public OwnerRestController(OwnerRestRepository repo) {this.repo = repo;}
@RequestMapping(value="/owner/add", method=RequestMethod.POST)
public Owner create(@RequestBody Map<String, String> body){
Owner o = new Owner();
o.setFirstName(body.get("firstName"));
o.setLastName(body.get("lastName"));
o.setAddress(body.get("address"));
o.setCity(body.get("city"));
o.setTelephone(body.get("telephone"));
this.repo.save(o);
return o;
}
}
Here is the Repository Interface
public interface OwnerRestRepository extends CrudRepository<Owner,integer>{}
Here is the JSON-Object Owner
{
"firstName":"fname",
"lastName":"lname",
"address":"address1",
"city":"city1",
"telephone":"4711"
}
Server Response
{
"id": 11,
"firstName": "fname",
"lastName": "lname",
"address": "address1",
"city": "city1",
"telephone": "4711"
}
What's wrong in the code that the data can't be stored in the database?
Best regards, Mux
Upvotes: 0
Views: 257
Reputation: 4617
By default Spring
runs the in-memory database - H2
, so your items only exist when your application is running.
To persist your data you need to configure the application to use another database.
To query H2
you can also use The H2 Console Application
For example if you want to configure MySQL
you should have add following configuration options to your application.properties
file.
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
In case your are using application.yml
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/test
username: dbuser
password: dbpass
Upvotes: 1