0
votes

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:

  1. 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);
    
    
     }
    
  2. NoteRepository.java (interface):

package com.example.demomysql;
    
    import org.springframework.data.repository.CrudRepository;
     
    public interface NoteRepository extends CrudRepository <Note, Integer>{
    
    }
  1. 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();
            }
            
        }
  1. 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:

enter image description here

I think I have a problem with Controller class. Please, can you help me?

Thanks

1
use @SpringBootApplication without exclusions and @ComponentScan and use @Controller and not @Restcontroller this should solve the 404Dirk Deyne
leave out spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver , don't need it, spring will detect from the connection-url, no need to provide a version either (pom). And make sure mysql is running... remark if you want to provide the driverClassName, make sure you use the correct one: com.mysql.jdbc.DriverDirk Deyne
I did your changes: I deleted the exclution and the @ComponentScan from my class Note Controller. Then, I corrected spring.datasource.driverClassName= com.mysql.jdbc.Driver. Now I have this problem on my console: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jdbc.mysql://localhost:3306/notedbSam

1 Answers

1
votes
pring.jpa.hibernate.ddl-auto=update
spring.datasource.platform=mysql
spring.datasource.url=jdbc:mysql://localhost:3306/notedb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=****
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

  • Remove

    • exclude={DataSourceAutoConfiguration.class}

    • @ComponentScan(basePackages={"com.joydeep.springboot"})

  • Keep @RestController remove @ResponseBody

  • For @Controller keep @ResponseBody or ResponseEntity<T>

  • Change return type Iterable<Note> to List<Note>

Application

@SpringBootApplication
public class DemoMysqlApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoMysqlApplication.class, args);
    }

Rest Controller

    @RestController
    @RequestMapping(value="/note")
    public class NoteController {

        @Autowired
        private NoteRepository noteRepository;

        @GetMapping(value="/debug")
        public List<Note> getNotes(){
            return noteRepository.findAll();
        }

    }

Controller

@Controller
    @RequestMapping(value="/note")
    public class NoteController {

        @Autowired
        private NoteRepository noteRepository;

        @GetMapping(value="/debug")
        public ResponseEntity<List<Note>> getNotes(){
             return new ResponseEntity<List<Note>>(noteRepository.findAll(),HttpStatus.OK);
        }

    }