this is my first exercise with Spring Boot and this is my application.properties:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc.mysql://localhost:3306/notedb
spring.datasource.username=root
spring.datasource.password=*******
These are my classes:
DemoMySqlApplication.java
package com.example.demomysql;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@ComponentScan(basePackages={"com.joydeep.springboot"}) public class DemoMysqlApplication {
public static void main(String[] args) { SpringApplication.run(DemoMysqlApplication.class, args); }
NoteRepository.java (interface):
package com.example.demomysql;
import org.springframework.data.repository.CrudRepository;
public interface NoteRepository extends CrudRepository <Note, Integer>{
}
- NoteController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@RestController
@RequestMapping(value="/note")
public class NoteController {
@Autowired
private NoteRepository noteRepository;
@GetMapping(value="/all")
public String getAllNotes(Model model) {
model.addAttribute("notes", noteRepository.findAll());
return "list";
}
@GetMapping(value="/debug")
public @ResponseBody Iterable<Note> getNotes(){
return noteRepository.findAll();
}
}
- Note.java (Entity class)
package com.example.demomysql;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.springframework.data.repository.CrudRepository;
@Entity
public class Note {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String title;
private String description;
private Boolean done;
public Integer getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean getDone() {
return done;
}
public void setDone(Boolean done) {
this.done = done;
}
}
From the console, I don't see errors. Tomcat starts normally. These are the last two information:
Tomcat started on port(s): 8080 (http) with context path ''
Started DemoMysqlApplication in 0.731 seconds (JVM running for 480.726)
But on my MySQL database (I created a DB named "notedb" and a table named "note", before to launch this application). I have one row with the data in note. But when I try to connect to:
http://localhost:8080/note/debug
I see:
I think I have a problem with Controller class. Please, can you help me?
Thanks