0

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

3
  • The objects are stored locally at runtime environment but not in the database. Commented Jan 1, 2019 at 11:43
  • So your question is: is it possible to store data in memory? Yes, of course it is. It's probably not a good idea, and you need to make it thread-safe, but sure, it's possible. Commented Jan 1, 2019 at 11:46
  • I could solve this issue to put these entries in application.properties file: spring.datasource.url=jdbc:mysql://anyurltodatabse spring.datasource.username=user spring.datasource.password=pwd spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver Commented Feb 14, 2019 at 21:33

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.