当我启动Postman看到我的SpringREST
服务正在运行时,我收到以下错误:
{
"timestamp": 1506965117328,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.transaction.CannotCreateTransactionException",
"message": "Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection",
"path": "/api/matchs"
}
我不知道错误在哪里,这里是项目的类:
的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>com.kaluzny</groupId>
<artifactId>spring-boot-rest-api-postgresql</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- PostgreSQL-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
<scope>runtime</scope>
</dependency>
<!-- Java EE -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Aplication.java
package com.kaluzny;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
MatchRestController.java
package com.kaluzny.web;
import com.kaluzny.domain.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.inject.Inject;
import java.util.Collection;
@RestController
@RequestMapping("/api/matchs")
public class MatchRestController {
private MatchRepository repository;
@Inject
public void setRepository(MatchRepository repository) {
this.repository = repository;
}
@RequestMapping(
method = RequestMethod.POST)
public ResponseEntity<?> addMatch(@RequestBody Match match) {
return new ResponseEntity<>(repository.save(match), HttpStatus.CREATED);
}
@RequestMapping(
method = RequestMethod.GET)
public ResponseEntity<Collection<Match>> getAllMatch() {
return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
}
@RequestMapping(
value = "/{idMatch}",
method = RequestMethod.GET)
public ResponseEntity<Match> getMatchWithIdMatch(@PathVariable Integer idMatch) {
return new ResponseEntity<>(repository.findOne(idMatch), HttpStatus.OK);
}
@RequestMapping(
value = "/{idMatch}",
method = RequestMethod.PUT)
public ResponseEntity<Match> updateMatchFromDB(@PathVariable("idMatch") Integer idMatch, @RequestBody Match match) {
Match currentMatch = repository.findOne(idMatch);
currentMatch.setIdMatch(match.getIdMatch());
currentMatch.setNumberPlayers(match.getNumberPlayers());
currentMatch.setWinner(match.getWinner());
currentMatch.setScore(match.getScore());
currentMatch.setNumberSpike(match.getNumberSpike());
currentMatch.setNumberFireball(match.getNumberFireball());
currentMatch.setNumberNuke(match.getNumberNuke());
Match currentMatch1 = repository.findOne(idMatch);
return new ResponseEntity<>(repository.save(currentMatch1), HttpStatus.OK);
}
@RequestMapping(
value = "/{idMatch}",
method = RequestMethod.DELETE)
public void deleteMatchWithId(@PathVariable Integer idMatch) {
repository.delete(idMatch);
}
@RequestMapping(
method = RequestMethod.DELETE)
public void deleteAllMatchs() {
repository.deleteAll();
}
}
MatchRepository.java
package com.kaluzny.domain;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import java.util.List;
@RepositoryRestResource
public interface MatchRepository extends JpaRepository<Match, Integer> {
List<Match> findByIdMatch(Integer idMatch);
}
Match.java
package com.kaluzny.domain;
import javax.persistence.*;
@Entity
public class Match {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer idMatch;
private Integer numberPlayers;
private String winner;
private Integer score;
private Integer numberSpike;
private Integer numberFireball;
private Integer numberNuke;
public Match(Integer idMatch, Integer numberPlayers, String winner, Integer score, Integer numberSpike, Integer numberFireball, Integer numberNuke) {
this.idMatch = idMatch;
this.numberPlayers = numberPlayers;
this.winner = winner;
this.score = score;
this.numberSpike = numberSpike;
this.numberFireball = numberFireball;
this.numberNuke = numberNuke;
}
public Match() {
}
public void setIdMatch(Integer idMatch){
this.idMatch = idMatch;
}
public Integer getIdMatch() {
return idMatch;
}
public void setNumberPlayers(Integer numberPlayers){
this.numberPlayers = numberPlayers;
}
public Integer getNumberPlayers(){
return numberPlayers;
}
public void setWinner(String winner){
this.winner = winner;
}
public String getWinner(){
return winner;
}
public void setScore(Integer score){
this.score = score;
}
public Integer getScore(){
return score;
}
public void setNumberSpike(Integer numberSpike){
this.numberSpike = numberSpike;
}
public Integer getNumberSpike(){
return numberSpike;
}
public void setNumberFireball(Integer numberFireball){
this.numberFireball = numberFireball;
}
public Integer getNumberFireball(){
return numberFireball;
}
public void setNumberNuke(Integer numberNuke){
this.numberNuke = numberNuke;
}
public Integer getNumberNuke(){
return numberNuke;
}
@Override
public String toString() {
return "Partida{" +
"Indentificador Partida: " + numberPlayers + '\'' +
"Numero Jogadores: " + numberPlayers + '\'' +
"Vencedor: "+ winner + '\'' +
"Pontuação: " + score + '\'' +
"Numero de Espinhos: " + numberSpike + '\'' +
"Numero de Fireball" + numberFireball + '\'' +
"Numero de Nukes" + numberNuke + '}';
}
}
正如我所说,我不知道问题出在哪里,我已经解决了其中的一些问题,但这个问题很困难。
谢谢大家。
答案 0 :(得分:0)
很明显,存在连接问题。请检查您的application.properties文件,看看数据库配置是否正确。