Search code examples
javaspring

Connecting MySQL to a Spring Boot REST Application


Based off a tutorial on the Spring site I was able to setup a simple demo REST application. It works fine but I just can't figure out how to tie it into my MySQL database. I have it configured to automatically connect but I know I am missing some glue. I have seen random blog posts about configuring a datasource bean in Application.java but I also read that using actuator this should all be done automatically. In the log output it looks like my database is being connected successfully, and when I hit the REST endpoints with cURL they work fine but just don't interact at all with my MySQL db. Am I missing the datasource? If so, could you provide guidance on getting it to work? Thanks!

The code is very simple:

Application.java package hello;

import ...

@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
@ComponentScan
@PropertySource("classpath:application.properties")
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

User.java

package hello;

import javax.persistence.*;

@Entity
@Table(name = "user")
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String username;

    public User()
    {  
    }

    public User(long id, String username)
    {
        this.id = id;
        this.username = username;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }
}

UserRepository.java

package hello;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends PagingAndSortingRepository<User, Long>
{
}

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/chrdb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver

Solution

  • You don't need the @PropertySources, @EnableJpaRepositories and @Import(RepositoryRestMvcConfiguration.class) as Spring Boot handles that for you already when Spring Data JPA and Spring Data REST are detected. (That is what @EnableAutoConfiguration is for).

    
    import ...
    
    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    

    Specifying the url, username and password should be enough (if you are using Spring Boot 1.1.x).

    spring.datasource.url=jdbc:mysql://localhost:3306/chrdb
    spring.datasource.username=root
    spring.datasource.password=
    

    Finally make sure you don't have H2, HQSQLDB or Derby on your classpath as those will automatically detected and might override your database settings.

    I have seen random blog posts about configuring a datasource bean in Application.java but I also read that using actuator this should all be done automatically.

    Autoconfiguration hasn't so much to do with the actuator (only for Spring Security, metrics and management) but with the auto configuration part of Spring Boot.