3

after searching for a long time i can't figure out how to work with spring-boot and postgresql. Spring-boot is starting without warnings. The postgres server is running, but i dont know if spring-boot can access it. Are the following files right ?

pom.xml :

 <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>org.springframework</groupId>
        <artifactId>gs-spring-boot</artifactId>
        <version>0.1.0</version>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
        </parent>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
            </dependency>
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.3.3.RELEASE</version>
            </dependency>
        </dependencies>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>


    </project>

application.properties :

spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/springbootdb
spring.datasource.username=postgres
spring.datasource.password=db

Entity :

@Entity
public class Frame implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String raw;
  private String time;
  private double tempRad;
  private double tempAirIn;
  private double tempAirOut;

  public Frame(String time, double tempRad, double tempAirIn, double tempAirOut){
    this.time = time;
    this.tempRad = tempRad;
    this.tempAirIn = tempAirIn;
    this.tempAirOut = tempAirOut;
  }
.
.
.

Application.java :

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        FrameServiceImpl frameServiceImpl = new FrameServiceImpl();
        int lower = 12;
        int higher = 29;
        for(int i = 0; i > 10000; i++){
            frameServiceImpl.save(new Frame(LocalDateTime.now().plusMinutes(i).toString(),
                (Math.random() * (higher-lower)) + lower,
                (Math.random() * (higher-lower)) + lower,
                (Math.random() * (higher-lower)) + lower));

        }
        SpringApplication.run(Application.class, args);
        System.out.println("Server started.");
    }

}

RestController :

@RestController
public class HelloController {

    @Autowired
    private FrameRepository frameRepository;

    @RequestMapping("/findall")
    public String findAll(){
        String result = "";
        int i = 0;

        for(Frame f : frameRepository.findAll()){
            result += i + " : " + f.toString() + "</br>";
            i++;
        }

        return "result : " + result;
    }
}

Postgresql server

From the browser

[EDIT] Now Working. - FrameServiceImpl needed @Component to be @Autowired - @Autowired component doesn't work in the main function (static) - @Autowired the FrameServiceImpl, dont instantiate it.

3
  • 1
    You could just add a couple of records to your frames table and look whether you see them in your controller output. Commented Jan 17, 2018 at 18:26
  • The "frame" table is empty and i don't know why. spring boot should set it up (see "spring.jpa.hibernate.ddl-auto=update"). you mean add them manually. I will try. Commented Jan 17, 2018 at 19:05
  • You were right. I added manually 2 lines to the db and i can see them in the browser. So my problem is saving them in the db. Commented Jan 17, 2018 at 19:25

1 Answer 1

6

In properties file I did this;

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/springbootdb
spring.datasource.username=postgres
spring.datasource.password=db
spring.jpa.database-platform=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false

and in pom;

<dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
</dependency>
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.