带有postgres sql的spring boot框架,运行时发生异常

Posted

技术标签:

【中文标题】带有postgres sql的spring boot框架,运行时发生异常【英文标题】:spring boot framework with postgres sql, An exception occurred while running 【发布时间】:2018-12-17 01:10:53 【问题描述】:

例外是:

An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'machineController': Unsatisfied dependency expressed through field 'machineRepository': Error creating bean with name 'machineRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property machine found for type Machine!; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'machineRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property machine found for type Machine!

编码的编辑器,是Subline,项目的代码

主要

springboot应用的主类

package com.brommarest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
importorg.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EntityScan(basePackages = "com.brommarest.entities" )
@EnableJpaRepositories(basePackages = "com.brommarest.repositories")
public class BrommaRestApplication 
public static void main(String[] args) 
    SpringApplication.run(BrommaRestApplication.class, args);

Repositories 存储库文件,MachineRepository.java 机器实体的存储库

package com.brommarest.repositories;
import com.brommarest.entities.Machine;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
import java.util.Set;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;

@Repository
public interface MachineRepository extends JpaRepository<Machine, Integer> 

// custom query to search to machine  by type or description
List<Machine> findBymachine_typeOrmachine_desc(String text, String 
textAgain);

entities 实体文件,Machine.java

存储库的实体类

package com.brommarest.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Machine 
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int machine_id;

private String machine_type;
private String machine_desc;
private String date_added;    

public Machine()   

public Machine(String machine_type, String machine_desc, String date_added) 

    this.setTitle(machine_type);
    this.setDesc(machine_desc);
    this.setDate(date_added);
   

public Machine(int machine_id, String machine_type, String machine_desc, 
String date_added) 
    this.setId(machine_id);
    this.setTitle(machine_type);
    this.setDesc(machine_desc);
    this.setDate(date_added);



public int getId() 
    return machine_id;


public void setId(int machine_id) 
    this.machine_id = machine_id;


public String getTitle() 
    return machine_type;


public void setTitle(String machine_type) 
    this.machine_type = machine_type;


public String getDesc() 
    return machine_desc;


public void setDesc(String machine_desc) 
    this.machine_desc = machine_desc;



public String getDate() 
    return date_added;


public void setDate(String date_added) 
    this.date_added = date_added;



@Override
public String toString() 
    return "Machine" +
            "machine_id=" + machine_id +
            ", machine_type='" + machine_type + '\'' +
            ", machine_desc='" + machine_desc + '\'' +
            ", date_added='" + date_added + '\'' +
            '';
 

控制器 MachineController.java

项目控制器

package com.brommarest.controllers;
import com.brommarest.entities.Machine;
import com.brommarest.repositories.MachineRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class MachineController 

@Autowired
MachineRepository machineRepository;

@GetMapping("/machine")
public List<Machine> index()
    return machineRepository.findAll();


@GetMapping("/machine/machine_id")
public Machine show(@PathVariable String machine_id)
    int machineId = Integer.parseInt(machine_id);
    return machineRepository.findOne(machineId);


@PostMapping("/machine/search")
public List<Machine> search(@RequestBody Map<String, String> body)
    String searchTerm = body.get("text");
    return machineRepository.findBymachine_typegOrmachine_desc(searchTerm, 
searchTerm);


@PostMapping("/machine")
public Machine create(@RequestBody Map<String, String> body)
    String machine_type = body.get("machine_type");
    String machine_desc = body.get("machine_desc");
    String date_added   = body.get("date_added");

    return machineRepository.save(new Machine(machine_type, machine_desc, 
date_added));


@PutMapping("/machine/machine_id")
public Machine update(@PathVariable String machine_id, @RequestBody 
Map<String, String> body)
    int machineId = Integer.parseInt(machine_id);
    // getting machine
    //Machine machine = MachineRepository.findOne(machineId);
    Machine machine = machineRepository.findOne(machineId);


    machine.setTitle(body.get("machine_type"));
    machine.setDesc(body.get("machine_desc"));

    return machineRepository.save(machine);
 

@DeleteMapping("/machine/machine_id")
public boolean delete(@PathVariable String machine_id)
    int machineId = Integer.parseInt(machine_id);
    machineRepository.delete(machineId);
    return true;


application.properties

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=default
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

pom 文件

<groupId>com.brommarest</groupId>
<artifactId>brommarest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Bromma-REST</name>
<description>Bromma-REST API'S project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath/>
    <!-- lookup parent from repository -->
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</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-devtools</artifactId>
        <optional>true</optional>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-social-twitter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</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-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-
 beanutils -->
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

   </plugins>
</build>

</project>

【问题讨论】:

没有任何代码,异常并没有真正的帮助。 不要将代码添加为 cmets,编辑您的问题。 尊重 Java 命名约定(属性中没有下划线),一切都会更好。另外,machine_id、machine_type等是Machine类的一个字段,所以“machine”前缀是多余的:machine.type是机器的类型:不需要machine.machine_type。 【参考方案1】:

接口签名应遵循命名约定。 Spring 根据方法签名生成查询,它不能。

我想您应该将属性名称大写。请试试这个:

List<Machine> findByMachine_typeOrMachine_desc(String text, String 
textAgain);

【讨论】:

尝试根据您的建议更改代码,但同样的错误 也正如@JB Nizet 所建议的那样 - 将属性名称重命名为不带下划线 + 我看到属性名称和设置器之间存在一些不一致:private String machine_type;但是 public String getTitle() return machine_type; public void setTitle(String machine_type) this.machine_type = machine_type; 【参考方案2】:

没有下划线(“_”)的业务是 Java,除非您知道自己在做什么。重命名所有方法和字段名称以使用 camelCase。 Spring 期望事情以某种方式发生,否则它会行为不端。

@GetMapping("/machine/machine_id")这个“_”很好

但是这个public Machine show(@PathVariable String machine_id)不是

【讨论】:

以上是关于带有postgres sql的spring boot框架,运行时发生异常的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 Postgres SQL 查询不使用索引

带有用户名和密码的 Zonky + Spring Boot + Postgres + Flyway

从java调用带有UDT参数的postgres函数给SQL尚未实现异常

spring boo的简单搭建(eclipse+springboot + redis + mysql + thymeleaf)

无法将 AWS-Postgres 服务器与带有 heroku 托管的 Spring Boot 应用程序连接起来

带有循环的 Postgres SQL 脚本