1
votes

I am attempting to use an H2 database while developing a Spring Boot application. I am using the Spring Data JPA starter.

build.gradle

    dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('commons-io:commons-io:2.5')    
    compile('org.springframework.security:spring-security-jwt:1.0.7.RELEASE')
    compile('org.springframework.security.oauth:spring-security-oauth2:2.2.1.RELEASE')
    // compile 'com.microsoft.sqlserver:mssql-jdbc:6.2.2.jre8'   
    runtime('org.springframework.boot:spring-boot-devtools')
    compile ('org.apache.httpcomponents:httpclient:4.5.5')
    testCompile('com.h2database:h2:1.4.196')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')    

}

The am using Java annotation to define the entities.

Example class:

@Entity
@Table(name="auth_user")
public class OAuthUser {

//    @Autowired 
//    @Transient
//    private PasswordEncoder passwordEncoder;
//    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name= "username")
    private String userName;

    @Column(name="password")
    @JsonIgnore
    private String password;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    @Column(name="email")
    private String email;

    @Column(name="is_enabled")
    private boolean isEnabled;

     /**
      * Reference: https://github.com/nydiarra/springboot-jwt/blob/master/src/main/java/com/nouhoun/springboot/jwt/integration/domain/User.java
     * Roles are being eagerly loaded here because
     * they are a fairly small collection of items for this example.
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_role", joinColumns
            = @JoinColumn(name = "user_id",
            referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id",
                    referencedColumnName = "id"))
private List<Role> roles;


    public OAuthUser() {};
    public OAuthUser(String firstName, String lastName, String user, String pass) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.userName = user;
        this.password = pass;
    }

/// Getters and Setters omitted

The properties (source):

spring.h2.console.enabled=true

spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:file:~/test

spring.datasource.username=sa

spring.datasource.password=

spring.datasource.driver-class-name=org.h2.Driver

However, whenever I visit http://localhost:8090/h2 and click Connect, I am simply directed to an empty white page. When I use the "Test Connection" button, it says "Test successful". As mentioned in this question I tried both jdbc:h2:mem:testdb and jdbc:h2:~/test for the JDBC URL.

No errors display in the IDE console. What might the issue be? Thanks.

1

1 Answers

9
votes

jdbc:h2:mem:testdb is the default URL. If you remove all the config regarding H2 from the properties it should run under this URL.

Edit: So if the config and the paths are correct, one more solution comes to my mind as i have noticed that you are using Spring Security. Try adding this to your security config : http.headers().frameOptions().disable();

For an example you can go there: http://shengwangi.blogspot.com/2016/02/how-to-use-h2-embeded-database-in-spring.html